Microsoft KB Archive/309603

= How to use OLE automation in Visio =

Article ID: 309603

Article Last Modified on 4/11/2007

-

APPLIES TO


 * Microsoft Office Visio Standard 2007
 * Microsoft Office Visio Professional 2007
 * Microsoft Office Visio Standard 2003
 * Microsoft Office Visio Professional 2003
 * Microsoft Visio 2002 Standard Edition
 * Microsoft Visio 2002 Professional Edition

-



This article was previously published under Q309603





INTRODUCTION
This article contains information about how to use Microsoft Visual Basic or Microsoft Visual Basic for Applications (VBA) to automate Microsoft Office Visio2from another Microsoft Office program.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. back to the top

Overview of automation
Automation (also called OLE automation) in Visual Basic is the process of controlling one program from another program or external development tool. You can automate any program that contains a Visual Basic object model. An object model is a hierarchical collection of the program's objects that are available or exposed to Visual Basic.

For example, the object model for Microsoft Visio contains objects such as:
 * Application/global object
 * Document object
 * Page object
 * Master object
 * Selection object
 * Shape object
 * Window object

Each of these objects has a unique set of methods and properties that are required to work with them in Visual Basic. For example, a Shape object can represent any object on a Visio drawing page that you can select by using the pointer. Therefore, a Shape object can be a shape, a group, a guide, a control, or an object from another program that is linked, embedded, or imported into a Visio drawing.

back to the top

Getting started
The code samples in this article demonstrate how to control Visio from Microsoft Office 2007, Microsoft Office 2003, Microsoft Office 2002, Microsoft Visual Basic 6.0, or from any program that includes a Visual Basic development tool. To automate Visio, follow these four main steps:
 * 1) Add a reference to the Visio Type Library.
 * 2) Declare a variable as a Visio object type (typically Visio.Application or Visio.Documents).
 * 3) Assign the GetObject or CreateObject method to the object variable you declared in step 2.
 * 4) Use the Visio object's properties, methods, and child objects to automate Visio.

back to the top

Step 1: Add a reference to the Visio Type Library
To add a reference to the Visio Type Library by using Microsoft Office applications such as Microsoft Office XP or Office 2003, follow these steps:
 * 1) In Access, PowerPoint, Excel, or Word, point to Macros on the Tools menu, and then click Visual Basic Editor.
 * 2) On the Tools menu, click References.

Note To add the reference using Microsoft Visual Basic 6.0, click References on the Project menu.
 * 1) In the list of Available References, click to select one of the following check boxes, depending on the version of Visio that you are using:
 * 2) * For Visio 2007, click to select the Microsoft Visio 12.0 Type Library check box.
 * 3) * For Visio 2003, click to select the Microsoft Visio 11.0 Type Library check box.
 * 4) * For Visio 2002, click to select the Microsoft Visio 2002 Type Library check box.

When you add the Microsoft Visio Type Library reference, your program can access Microsoft Visio Online Help and the Visio object model. Because the references are saved in each project, you have to add the Visio Type Library reference for each Visual Basic or VBA project that you want to use to automate Visio.

back to the top

Step 2: Declare the object variable
To declare a Visio object variable, dimension a variable as a specific Visio object type, such as Visio.Application, Visio.Documents, or Visio.Page.

Explicitly declaring the object type is called early binding because the controller application connects or binds the object to the Visio application at compile-time rather than at run-time. This gives you access to Visio auto lists and context sensitive Help, and allows the code to run more efficiently.

For more information about object binding, click the following article number to view the article in the Microsoft Knowledge Base:

138138 Late, ID, Early Binding types possible in VB for Apps

The following sample Visual Basic argument declares the variable AppVisio as an object of type Visio.Application:

Dim AppVisio as Visio.Application back to the top

Step 3: Set the variable
You can use the following two Visual Basic methods to activate Visio:
 * CreateObject
 * GetObject

The primary difference is that the CreateObject method creates a new instance of Visio, and the GetObject method uses an already running instance of Visio. You can also use GetObject to set your object variable to a specific Visio document.

The following sample argument sets the AppVisio variable to the Visio application using the CreateObject function: Dim AppVisio as Visio.Application

Set AppVisio = CreateObject(&quot;Visio.Application&quot;) In some cases, you might want to use an existing Visio instance if Visio is already running, but create a new instance if Visio is not running. To do this, create an error handler that uses the CreateObject method in the event that the GetObject method fails, as shown in this sample code: Dim AppVisio As Visio.Application

On Error Resume Next

Set AppVisio = GetObject(, &quot;visio.application&quot;)

If AppVisio Is Nothing Then Set AppVisio = CreateObject(&quot;visio.application&quot;) End If

Note You can also use the CreateObject function to create a Visio instance that is invisible. For example: Set AppVisio = CreateObject(&quot;Visio.InvisibleApp&quot;) You can then use the Application object's Visible property to control whether the instance is visible.

You can use the InvisibleApp object with only the CreateObject function. Attempts to use it with the GetObject function will fail. The InvisibleApp object is not available in versions of Visio earlier than Microsoft Visio 2000.

back to the top

Step 4: Use the Visio objects, methods, and properties
After you complete steps 1 through 3, you can use the Visio object variable to automate Visio.

The following sample macro uses automation to start Visio, create a new drawing (document) based on the Basic Diagram template, drops a rectangle, adds some text, and saves the drawing and quits Visio. Sub AutoVisio

Dim AppVisio As Visio.Application ' Declare an Instance of Visio. Dim docsObj As Visio.Documents    ' Documents collection of instance. Dim DocObj As Visio.Document      ' Document to work in. Dim stnObj As Visio.Document      ' Stencil that contains master. Dim mastObj As Visio.Master       ' Master to drop. Dim pagsObj As Visio.Pages        ' Pages collection of document. Dim pagObj As Visio.Page          ' Page to work in. Dim shpObj As Visio.Shape         ' Instance of master on page.

' Create an instance of Visio and create a document based on the ' Basic Diagram template. It doesn't matter if an instance of  ' Visio is already running, CreateObject will run a new one. Set AppVisio = CreateObject(&quot;visio.application&quot;)

Set docsObj = AppVisio.Documents

' Create a document based on the Basic Diagram template that ' automatically opens the Basic Shapes stencil. Set DocObj = docsObj.Add(&quot;Basic Diagram.vst&quot;)

Set pagsObj = AppVisio.ActiveDocument.Pages

' A new document always has at least one page, whose index in the ' Pages collection is 1. Set pagObj = pagsObj.Item(1)

Set stnObj = AppVisio.Documents(&quot;Basic Shapes.vss&quot;) Set mastObj = stnObj.Masters(&quot;Rectangle&quot;)

' Drop the rectangle in the approximate middle of the page. ' Coordinates passed with the Drop method are always inches. Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)

' Set the text of the rectangle. shpObj.Text = &quot;This is some text.&quot;

' Save the drawing and quit Visio. The message pauses the program ' so you can see the Visio drawing before the instance closes. DocObj.SaveAs &quot;MyDrawing.vsd&quot; MsgBox &quot;Drawing finished!&quot;,, &quot;AutoVisio (OLE) Example&quot;

' Quit Visio. AppVisio.Quit

' Clear the variable from memory. Set AppVisio = Nothing

End Sub back to the top



Microsoft Visio Developer Web sites
For more information about how to automate Visio, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/office/aa905478.aspx

For more information about how to develop Microsoft Visio solutions, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/aa217846(office.10).aspx

Visio Software Development Kits
To download the Visio 2002 Software Development Kit, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?familyid=d2845e80-6634-4703-9ad9-7e440ede12d7&displaylang=en

To download the Visio 2003 Software Development Kit, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?familyid=557120bd-b0bb-46e7-936a-b8539898d44d&displaylang=en

NewsGroups
The following peer-to-peer newsgroup is available to help you interact with other users of Visual Basic for Applications:

microsoft.public.vb.ole.automation

microsoft.public.visio.developer.vba

Visual Basic Help
For more information about how to use the CreateObject function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type createobject function in the Search box, and then click Search to view the topics that are returned.

For more information about how to use the GetObject function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type getobject function in the Search box, and then click Search to view the topics that are returned.

back to the top

Additional query words: vb vba vbe OLE automation macro GetObject CreateObject sample example vso2002 vso2003 vso2007

Keywords: kbautomation kbprogramming kbvba kbhowto KB309603

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.