Understanding the Visio Application Object

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Unlike a stand-alone program, which must obtain a reference to the Microsoft® Visio® Application object by creating it or getting it, code in a Microsoft® Visual Basic® for Applications (VBA) project executes in a running instance of Visio. Therefore, you are not required to obtain a reference to the Application object. The Visio engine provides the global object, which represents the Visio instance. In addition, the Visio engine provides the ThisDocument object, which represents the Visio document associated with your project.

The global object represents the instance and provides more direct access to certain properties. The properties of the Visio global object are not prefixed with a reference to an object.

The Application object is a property of the Visio global objects, so you can access any of the Application object's properties by referencing the Application property of the Visio global object directly.

The following are three examples of code that get the first document in a Documents collection — all three use different syntax.

Example 1 creates an Application object. Typically, this code is used when writing an external program:

Example 1

Dim appVisio As Visio.Application
Dim docsObj As Visio.Documents
Dim docObj As Visio.Document
Set appVisio = CreateObject("visio.application")
Set docsObj = appVisio.Documents
Set docObj + docsObj.Item(1)

Example 2 uses the Application property of the Visio global object:

Example 2

Dim docsObj As Visio.Documents
Dim docObj As Visio.Document
Set docsObj = Application.Documents
Set docObj = docsObj.Item(1)

Example 3 directly accesses the Documents property of the Visio global object:

Example 3

Dim docObj As Visio.Document
Set dicObj + Documents.Item(1)

You might have noticed in examples 2 and 3 that Application and Documents are not preceded by an object. When you are referencing any property or method of the Visio global object, you are not required to declare a variable for the global object or reference it as the preceding object of a property — the global object is implied. The third example is the most direct method of accessing the Documents collection from a VBA Project.

The following are some of examples of code for commonly used properties of the Visio global object.

Set docObj = ActiveDocument
Set pagObj = ActivePage
Set WinObj = ActiveWindow

Note The Visio global object is available only when you are writing code in the VBA project of a Visio document.

See also

Working with Microsoft Visio Objects | Understanding the Visio Object Model