VBA Visio
Visio is a Microsoft application used for diagramming. VBA is embedded in the Visio application and can be used to work with Visio programmatically. The Visio Object Library contains classes that make working with Visio in VBA possible. The classes in the Visio Object Library are referred to collectively as the Visio Object Model. When using VBA within Visio, a reference is automatically included to the Visio Object Library.
Visio Object Model
The Visio Object Model contain classes that make working with Visio in VBA possible. The Visio Object Model contains many classes but the general overview of the model is Application → Document → Page → Shape.
Context
The place where VBA code is written determines the implied context when referring to classes. For example, when using VBA in Visio and referring to the Application object, it is implied that Application and Visio.Application are the same object. However, when using VBA in another application to work with Visio, it is necessary to qualify references to avoid naming collisions with classes in the other application. Using qualified references requires that a reference is set to the library where the classes are defined. If a reference cannot be set, late-binding can be used instead.
Public Sub Example()
'''In Visio
Debug.Print Application Is Visio.Application 'Prints: True
End Sub
Public Sub Example()
'''In Excel
'Reference to Visio Object Library is required
Debug.Print Application Is Visio.Application 'Prints: False
Debug.Print Application Is Excel.Application 'Prints: True
End Sub
To retrieve the Visio Application object from another application, attempt to retrieve the running application and if that fails start a new instance of the application.
Public Function GetVisioApp() As Object
On Error Resume Next
Set GetVisioApp = GetObject(, "Visio.Application")
On Error GoTo 0
If GetVisioApp Is Nothing Then
Set GetVisioApp = CreateObject("Visio.Application")
End If
End Function
Application
The Application object represents the Visio application itself. The Application object is the top-level object and provides access to the rest of the Visio Object Model. The Application object can be used to access functionality and properties of the Visio application.
Documents
The Document object represents a document in Visio. The Documents collection object contains a collection of Document objects. Use the Application.Documents property to get a Documents collection containing all open Documents.
Add Document
To add a document use the Documents.Add method or Documents.AddEx method.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents.Add
End Sub
Save Document
To save a document use the SaveAs, SaveAsEx, and Save methods.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim FilePath As String
FilePath = Environ$("USERPROFILE") & "\Desktop\ExampleDocument.vsdx"
D.SaveAs FilePath
End Sub
Open Document
To open a document use the Documents.Open or Documents.OpenEx method.
Public Sub Example()
Dim FilePath As String
FilePath = Environ$("USERPROFILE") & "\Desktop\ExampleDocument.vsdx"
Dim D As Visio.Document
Set D = Visio.Application.Documents.Open(FilePath)
End Sub
Close Document
To close a document use the Close method.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
D.Close
End Sub
Export Document
To export a document as a PDF or XPS document use the ExportAsFixedFormat method. Use the VisFixedFormatTypes enum to determine the export format.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim FilePath As String
FilePath = Environ$("USERPROFILE") & "\Desktop\ExamplePDF.pdf"
D.ExportAsFixedFormat visFixedFormatPDF, FilePath
End Sub
Pages
The Page object represents a page in a Document. The Pages collection object contains a collection of Page objects. Use the Document.Pages property to get a Pages collection containing all Pages in a Document.
Add Page
To add a page use the Pages.Add method.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim P As Visio.Page
Set P = D.Pages.Add
End Sub
Delete Page
To delete a page use the Delete method.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim P As Visio.Page
Set P = D.Pages(1)
P.Delete
End Sub
Duplicate Page
To duplicate a page use the Duplicate method.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim P As Visio.Page
Set P = D.Pages(1)
Dim P1 As Visio.Page
Set P1 = P.Duplicate
End Sub
Export Page
To export a page use the Export method.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim P As Visio.Page
Set P = D.Pages(1)
P.Export Environ$("USERPROFILE") & "\Desktop\ExampleExport.bmp"
End Sub
Masters
The Master object represents a master on a stencil. The Masters collection object contains a collection of Master objects. Use the Document.Masters property to get a Masters collection containing all Master objects in a Document.
Add Master
To add a master use the Masters.Add and Masters.AddEx methods. Use a member of the VisMasterTypes enum with the Masters.AddEx method to determine the type of master to add.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim M As Visio.Master
Set M = D.Masters.Add
End Sub
Delete Master
To delete a master use the Delete method.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim M As Visio.Master
Set M = D.Masters.Add
M.Delete
End Sub
Add Shape to a Master
To add a Shape to a master use one of the Draw methods.
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim M As Visio.Master
Set M = D.Masters.Add
Dim S As Visio.Shape
Set S = M.DrawRectangle(1, 2, 2, 1)
End Sub
Shapes
The Shape object represents a shape in Visio. The Shapes collection object contains a collection of Shape objects. Use the Page.Shapes property to get a Shapes collection containing all Shapes on a Page and use the Master.Shapes property to get a Shapes collection containing all Shapes in a Master.
Add Shape
Shapes can be created in different ways. A Shape can be drawn using one of the Draw methods. A Shape can also be created and then added to a page using a Master object and the Page.Drop method.
Public Sub Example()
Dim P As Visio.Page
Set P = Visio.Application.Documents(1).Pages(1)
Dim S As Visio.Shape
Set S = P.DrawRectangle(1, 2, 2, 1)
Dim S1 As Visio.Shape
Set S1 = P.Drop(S, 5, 5)
End Sub
Public Sub Example()
Dim D As Visio.Document
Set D = Visio.Application.Documents(1)
Dim M As Visio.Master
Set M = D.Masters.Add
Dim S As Visio.Shape
Set S = M.DrawRectangle(1, 2, 2, 1)
Dim P As Visio.Page
Set P = D.Pages(1)
Dim S1 As Visio.Shape
Set S1 = P.Drop(S, 1, 1)
End Sub
Copy, Cut, and Paste
To copy a Shape use the Copy method. To cut a Shape use the Cut method. To paste a Shape use the Page.Paste method.
Public Sub Example()
Dim P As Visio.Page
Set P = Visio.Application.Documents(1).Pages(1)
Dim S As Visio.Shape
Set S = P.DrawRectangle(1, 2, 2, 1)
S.Copy
P.Paste
S.Cut
P.Paste
End Sub
Delete Shape
To delete a Shape use the Delete method.
Public Sub Example()
Dim P As Visio.Page
Set P = Visio.Application.Documents(1).Pages(1)
Dim S As Visio.Shape
Set S = P.DrawRectangle(1, 2, 2, 1)
S.Delete
End Sub
Layers
The Layer object represents a layer. The Layers collection object contains a collection of Layer objects. Use the Page.Layers property to get a Layers collection containing all Layer objects for a Page.
Add Layer
To add a layer use the Layers.Add method.
Public Sub Example()
Dim P As Visio.Page
Set P = Visio.Application.Documents(1).Pages(1)
Dim L As Visio.Layer
Set L = P.Layers.Add("Layer1")
End Sub
Add Shape to Layer
To add a shape to a layer use the Add method and pass a Shape object.
Public Sub Example()
Dim P As Visio.Page
Set P = Visio.Application.Documents(1).Pages(1)
Dim S As Visio.Shape
Set S = P.Shapes(1)
Dim L As Visio.Layer
Set L = P.Layers("Layer1")
L.Add S, 0
End Sub
Remove Shape from Layer
To remove a shape from a layer use the Remove method.
Public Sub Example()
Dim P As Visio.Page
Set P = Visio.Application.Documents(1).Pages(1)
Dim S As Visio.Shape
Set S = P.Shapes(1)
Dim L As Visio.Layer
Set L = P.Layers("Layer1")
L.Remove S, 1
End Sub
Delete Layer
To delete a layer use the Delete method.
Public Sub Example()
Dim P As Visio.Page
Set P = Visio.Application.Documents(1).Pages(1)
Dim S As Visio.Shape
Set S = P.Shapes(1)
Dim L As Visio.Layer
Set L = P.Layers("Layer1")
L.Delete 0
End Sub