Finally, with the release of Visio 2003, came Visio’s most useful tool for development; the macro recorder. By clicking the record button it is possible to quickly see how various Visio tasks can be translated into VBA. The Help file that comes with Visio is useful for explaining the details of a command, but there is not enough examples explaining how to string several commands together, the macro recorder fills in this gap.
For example, to simply answer the question; “How do you create two shapes and connect them together using VBA?” The macro recorder will generate the following macro.
Sub Macro1()
Application.ActiveWindow.Page.DrawRectangle 1#, 10#, 2#, 9#
Dim vsoCharacters1 As Visio.Characters
Set vsoCharacters1 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).Characters
vsoCharacters1.Begin = 0
vsoCharacters1.End = 0
vsoCharacters1.Text = “John”
Application.ActiveWindow.Page.DrawRectangle 3#, 10#, 4#, 9#
Dim vsoCharacters2 As Visio.Characters
Set vsoCharacters2 = Application.ActiveWindow.Page.Shapes.ItemFromID(2).Characters
vsoCharacters2.Begin = 0
vsoCharacters2.End = 0
vsoCharacters2.Text = “Mary”
Dim UndoScopeID3 As Long
UndoScopeID3 = Application.BeginUndoScope(“Drop On Page”)
Application.Windows.ItemEx(“atest.vsd”).Activate
Application.ActiveWindow.Page.Drop Application.Documents.Item(“C:\DOCUME~1\John\MYDOCU~1\VISIOC~1\atest.vsd”).Masters.ItemU(“Dynamic connector”), 0#, 0#
Dim vsoCell1 As Visio.Cell
Dim vsoCell2 As Visio.Cell
Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(3).CellsU(“BeginX”)
Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(3).CellsU(“EndX”)
Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(2).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
Application.EndUndoScope UndoScopeID3, True
End Sub
That sure looks like a mouthful, but it can quickly reduced to a more manage size by a few simple edits.
The macro recorder will record the actions taken and the extra code required to undo the changes. This extra code is easily identified and can be quickly removed. Each block of undo code uses a variable with a name like UndoScopeIDn, where n is a number that gets incremented for each undo bloc. Each block contains three commands; a Dim, Application.BeginUndoScope and a Application.EndUndoScope statement. These statements can be removed.
The references to Application.ActiveWindow.Page can be replaced with a reference to ActivePage.
The macro recorder uses the more formal form of Shapes.ItemFromID(1), but this can be simplified to Shapes(1).
For text handling you will probably find something like:
Set vsoCharacters1 = ActivePage.Shapes(1).Characters
vsoCharacters1.Begin = 0
vsoCharacters1.End = 0
vsoCharacters1.Text = “John”
This is useful if you are manipulating the shapes text, but if, as in this case, you are replacing the entire text, this long winded block of text can be replaced with:
ActivePage.Shapes(1).Text = “John”
Now, the macro looks like:
Sub Macro1()
ActivePage.DrawRectangle 1#, 10#, 2#, 9#
ActivePage.Shapes(1).Text = “John”
ActivePage.DrawRectangle 3#, 10#, 4#, 9#
ActivePage.Shapes(2).Text = “Mary”
Application.Windows.ItemEx(“atest.vsd”).Activate
ActivePage.Drop Application.Documents.Item(“C:\DOCUME~1\John\MYDOCU~1\VISIOC~1\atest.vsd”).Masters.ItemU(“Dynamic connector”), 0#, 0#
Dim vsoCell1 As Visio.Cell
Dim vsoCell2 As Visio.Cell
Set vsoCell1 = ActivePage.Shapes(3).CellsU(“BeginX”)
Set vsoCell2 = ActivePage.Shapes(1).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
Set vsoCell1 = ActivePage.Shapes(3).CellsU(“EndX”)
Set vsoCell2 = ActivePage.Shapes(2).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
End Sub
This makes it a lot easier to understand the generated code and relate it back to the information that is provided in the help file.
John… Visio MVP