[Note: This is the fourth in a series of blog posts that highlight some of the new members of the Visio 2010 VBA object model.]
In the first post in this series, I talked about the Page.DropConnected method. In the second post, I featured the Page.LayoutChangeDirection method, which is another new method on the Page object of the Visio 2010 VBA object model that you might not yet have encountered. The Page.LayoutChangeDirection method makes it possible to rotate or flip a set of two or more connected shapes on the page as a unit, without having to rotate or flip the individual shapes. In the most recent post, I featured the Page.AutoConnectMany method, which you can use to automatically connect multiple shapes as you drop them on the drawing page.
This post focuses on yet another new method on the Page object, the Page.DropCallout method, which creates a new callout shape on the page and associates the callout with the target shape you specify.
The syntax for this method is as follows:
Page.DropCallout(ObjectToDrop As Unknown, TargetShape As Shape)
The DropCallout method operates on a Page object and takes two required parameters:
The method returns a Shape object that represents the callout added to the page.
The DropCallout method corresponds to the Callout command on the Insert tab in the Visio 2010 user interface:
When you click Callout, the Callout Gallery appears:
This gallery displays the various built-in callout shapes available in Visio 2010. When you pause the mouse over a callout shape, Visio both displays the name of the callout and previews its appearance. For example, here’s what the Word balloon callout looks like in preview:
It takes two steps to access these callout shapes programmatically:
In the example code that follows, both of these steps happen in the same line of code, as you will see.
Here’s an example of how the DropCallout method works. Suppose you have a blank Visio drawing and you want to first add a rectangle to the page and then add a Word balloon callout associated with the rectangle. Paste the following code into the Visual Basic Editor code window (to open it, press ALT+F11) and then run it:
Public Sub DropCallout_Example()
Dim vsoDocument As Visio.Document Dim vsoTargetShape as Visio.Shape Set vsoDocument = Application.Documents.OpenEx(Application.GetBuiltInStencilFile(visBuiltInStencilCallouts, visMSUS), visOpenHidden) Set vsoTargetShape = ActivePage.DrawRectangle(3, 4, 6, 7) Application.ActivePage.DropCallout vsoDocument.Masters.ItemU("Word balloon"), vsoTargetShape vsoDocument.Close
Dim vsoDocument As Visio.Document
Dim vsoTargetShape as Visio.Shape
Set vsoDocument = Application.Documents.OpenEx(Application.GetBuiltInStencilFile(visBuiltInStencilCallouts, visMSUS), visOpenHidden)
Set vsoTargetShape = ActivePage.DrawRectangle(3, 4, 6, 7)
Application.ActivePage.DropCallout vsoDocument.Masters.ItemU("Word balloon"), vsoTargetShape
vsoDocument.Close
End Sub
Your diagram should now look like this:
Additionally, you can access callout information about shapes and specify callout relationships between shapes by using three new properties on the Shape object: