Understanding the Visio Object Model
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.
The Microsoft® Visio® object model represents the objects, properties, methods, and events that the Visio engine exposes through automation. More important, it describes how the objects are related to each other.
Most objects in the model correspond to items you can see and select in the Visio user interface. For example, a Shape object can represent anything on a Visio drawing page that you can select with the pointer tool — a shape, a group, a guide, or an object from another application that is linked, embedded, or imported into a Visio drawing.
Visio objects reside in an instance of the Visio application. Microsoft® Visual Basic® for Applications (VBA) code runs within an instance of Visio and accesses the objects it requires. An external program runs outside an instance of the Visio application, so it starts the Visio application or accesses an instance of Visio that is running, and then it accesses the Visio object it needs.
Some objects represent a collection of other objects. A collection contains zero or more objects of a specified type. For example, a Document object represents one open document in an instance of Visio; the Documents collection represents all of the documents that are open in the instance.
Using Visio Object Types
You can take advantage of the Visio type library to write code more effectively. By using Visio object types declared in the type library, you can declare variables as specific types, such as Visio.Page
, which is illustrated in the following code:
Dim pagObj As Visio.Page
This example uses Visio
to inform the program that it is referencing Visio object types in the Visio type library, and it uses Page
to inform the program that the pagObj
variable is a Page object. Here are a few more object types:
Dim docsObj As Visio.Documents 'A Documents collection
Dim docObj As Visio.Document 'A Document object
Dim shpsObj As Visio.Shapes 'A Shapes collection
Dim shpObj As Visio.Shape 'A Shape object
Dim mastObj As Visio.Master 'A Master object
Getting and Releasing Visio Objects
You get an object by declaring an object variable, navigating through the object model to get a reference to the object you want to control, and assigning the reference to the object variable. After you have a reference to an object, you can get and set the values of its properties or use methods that cause the object to perform actions.
The following are some guidelines for getting and releasing Visio objects:
- **Declare object variables **Declare a Visio object type as defined in the Visio type library. Use the Set statement to assign the reference to the object variable.
- **Access Visio objects through properties **Most Visio objects have properties whose values refer to other objects. You can use these properties to navigate up and down the layers of the object model to get to the object you want to control.
- **Refer to an object in a collection **A collection is an object that represents objects of a particular type. You can get a reference to a particular object in the collection. The Item property returns a reference to an object in the collection.
- **Iterate through a collection **A collection's Count property returns the number of objects in the collection. Most often, you will use the Count property to set the limit for an iteration loop.
- **Release an object **An object in a collection is released automatically when the program finishes running or when all object variables referring to that object go out of scope.
- **Use compound object references **You can concatenate Visio object references, properties, and methods in single statements as you can with Microsoft® Visual Basic® for Applications (VBA) objects. However, simple references are sometimes more efficient, even if they require more lines of code.
- **Restrict the scope and lifetime of object variables **You can prevent invalid references by restricting the scope and lifetime of an object variable. For example, when your program resumes execution after giving control to the user, you can release certain objects and retrieve them again to make sure that the objects still are available and your program has references to the objects in their current state.
See also
Working with Microsoft Visio Objects | Understanding the Visio Application Object