Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Sample VBA Macro (Visio)

Last modified: June 20, 2011

Applies to: Office 2013 | Visio 2013

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

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

Here is an example of the program's output, assuming drawings named Office.vsd and Recycle.vsd are open and have been saved in the specified locations. The locations shown are not those in which Visio saves drawings by default.

Sample output

Description

C:\documents\drawings\Office.vsd

The name of the first drawing

Background-1

The name of page 1

Background-2

The name of page 2

C:\documents\drawings\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 in the VBA environment and about the Visual Basic Editor in Microsoft Visual Basic Help (in the Visual Basic Editor window, on the Help menu, click Microsoft Visual Basic Help).

You can find details about using a specific Visio object, property, method, enumeration, or event in this reference.

Show:
© 2015 Microsoft