Use the Page.LayoutChangeDirection and the Selection.LayoutChangeDirection methods to change the orientation of shapes in Visio 2010
[Note: This is the second in a series of blog posts that highlight some of the new members of the Visio VBA OM.]
In the first post in this series, I talked about the Page.DropConnected method. In this post, I’ll present another new method on the Page object of the Visio 2010 VBA API that you might have overlooked, the Page.LayoutChangeDirection method, which 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.
The syntax for this method is as follows:
Page.LayoutChangeDirection(Direction)
As you can see, the method takes a single parameter, Direction, which should be a constant from the VisLayoutDirection enumeration. The four constants in this enum specify rotations of 90 degrees clockwise or counterclockwise, or vertical or horizontal flips.
Here’s an example of how this method might work in the Visio UI. Suppose you have three connected shapes on the page:
If you pass to the LayoutChangeDirection method the visLayoutDirFlipVert constant, your diagram will look like this:
The following VBA code is all it takes:
Public Sub PageLayoutChangeDirection_Example()
ActivePage.LayoutChangeDirection (visLayoutDirFlipVert)
End Sub
This code assumes that there are at least two connected shapes on the page. If you have more than one set of connected shapes on the page, the method will work simultaneously on all of them.
If that’s not what you want to happen, you can use another new method on the Selection object, Selection.LayoutChangeDirection, to limit the activity to the shapes you select.
This method has similar syntax to the method on the Page object:
Selection.LayoutChangeDirection(Direction)
It works in much the same way. Select these shapes:
Then run the following code:
Public Sub SelectionLayoutChangeDirection_Example()
Dim vsoSelection As Visio.Selection
Set vsoSelection = ActiveWindow.Selection
vsoSelection.LayoutChangeDirection (visLayoutDirFlipVert)
End Sub
The shapes will then appear like this:
Notice that the shapes themselves aren’t flipped—only their location relative to one another. So, as a result, the pentagon remains point up. This produces an offset of the connector, perhaps not what you intended. Of course, this might not be a problem with other, more symmetrical shapes.