Sample Microsoft VBA macro

Microsoft Visio Developer Reference

Sample Microsoft VBA macro

For each drawing file that is open in the Microsoft Visio instance, the sample Microsoft Visual Basic for Applications (VBA) macro shown below does the following:

  • Logs the name and path of the drawing file in the Immediate window
  • Logs the name of each page in the Immediate window

Here is a look at the code in the program and what it does.

Public Sub ShowNames ()
    'Declare object variables as Visio object types
    Dim pagObj As Visio.Page 
    Dim docObj As Visio.Document
    Dim docsObj As Visio.Documents
    Dim pagsObj As Visio.Pages
    'Iterate through all open documents
    Set docsObj = Application.Documents
    For Each docObj In docsObj
        'Print the drawing name in the Visual Basic Editor
        'Immediate Window
        Debug.Print docObj.FullName
        'Iterate through all pages in a drawing
        Set pagsObj = docObj.Pages
        For Each pagObj In pagsObj
            'Prints the page name in the Visual Basic Editor
            'Immediate Window
            Debug.Print Tab(5); pagObj.Name
        Next
    Next
End Sub

Here is an example of the program's output, assuming Office.vsd and Recycle.vsd are open and have been saved in the specified locations.

Sample output

Description

c:\visio\solutions\Office.vsd

The name of the first drawing

      Background-1

The name of page 1

      Background-2

The name of page 2

c:\visio\solutions\Recycle.vsd

The name of the second drawing

      Page-1

The name of page 1

      Page-2

The name of page 2

      Page-3

The name of page 3

You can find more information about writing a program using the VBA environment and about the Visual Basic Editor in the Microsoft Visual Basic Help (in the Visual Basic Editor window, click Microsoft Visual Basic Help on the Help menu).

You can find details about using a specific Visio object, property, method, or event in the Automation Reference included in this Developer Reference (on the Visio Help menu, click Developer Reference).

Note If you did not install the Developer Reference at the time you installed Visio, clicking the Developer Reference command on the Help menu will automatically start its installation.