Using the Global and ThisDocument Objects

3 4

Unlike a stand-alone program, which needs to obtain a reference to the Microsoft Visio Application object by creating or getting it, code in a Microsoft Visual Basic for Applications (VBA) project executes in a running Visio instance so you don't need to obtain a reference to the Application object. The Visio engine provides the global object, which represents the Visio instance. The Visio engine also provides the ThisDocument object, which represents the Visio document associated with your project.

Using the Visio Global Object

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.

figure 15-9. the visio global object and its properties.

Figure 15-9 The Visio global object and its properties.

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

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

This first example creates an Application object. This code is typically used when writing an external program:

 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) 

This second example uses the Application property of the Visio global object:

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

This third example directly accesses the Documents property of the Visio global object:

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

Notice in the second and third examples that Application and Documents are not preceded by an object. When you are referencing any property or method of the Visio global object, you don't need 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 examples show code for commonly used properties of the Visio global object:

 Set docObj = ActiveDocument Set pagObj = ActivePage Set winObj = ActiveWindow 

For details about the Visio global object's properties and methods, see the Automation Reference in Microsoft Visio (on the Help menu, click Developer Reference).

Note


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

Using the ThisDocument Object

Every VBA project in your Visio application contains a default class module called ThisDocument, which represents the properties, methods, and events of the document associated with the project. Like any class module, other programs can access ThisDocument at run time.

Figure 15-10.  Use the <b>ThisDocument</b> object to manipulate the document associated with your VBA project

Figure 15-10 Use the ThisDocument object to manipulate the document associated with your VBA project

If you want to manipulate a document, but not necessarily the document associated with your VBA project, get the Document object from the Documents collection. If you want to manipulate the document associated with your VBA project, use the ThisDocument object.

For example, to reference the first page of the drawing Hello.vsd, you could get the Document object from the Documents collection of the global object. The following example gets the first page of Hello.vsd from the Pages collection of the document:

 Set docObj = Documents.Item("hello.vsd") Set pagObj = docObj.Pages.Item(1) 

If Hello.vsd is the document associated with your VBA project, you could just use the ThisDocument object as the following example does:

 Set pagObj = ThisDocument.Pages.Item(1) 

Once you have a reference to a Document object, you retrieve other Visio objects by getting properties of the Document object, and then of other objects in the object hierarchy.

You can add properties and methods to the ThisDocument object because it is an extensible object—an object whose functionality you can extend. The ThisDocument object is the only extensible object the Visio engine provides. You can also select ThisDocument in the Project Explorer and change its properties, such as page settings and default styles; in the Properties window, you can also change the document properties, such as title, creator, and subject.

For details about properties, methods, and events for ThisDocument, select the ThisDocument object in the Project Explorer for the project you are interested in, open the Object Browser, view the Visio project containing ThisDocument in the project list, and browse the members of ThisDocument.



Developing Microsoft Visio Solutions 2001
Developing Microsoft Visio Solutions (Pro-Documentation)
ISBN: 0735613532
EAN: 2147483647
Year: 2004
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net