How to use OLE automation in Visio
This article was previously published under Q309603
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 automationAutomation (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
back to the top
Getting startedThe 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:
- Add a reference to the Visio Type Library.
- Declare a variable as a Visio object type (typically Visio.Application or Visio.Documents).
- Assign the GetObject or CreateObject method to the object variable you declared in step 2.
- Use the Visio object's properties, methods, and child objects to automate Visio.
Step 1: Add a reference to the Visio Type LibraryTo add a reference to the Visio Type Library by using Microsoft Office applications such as Microsoft Office XP or Office 2003, follow these steps:
- In Access, PowerPoint, Excel, or Word, point to Macros on the Tools menu, and then click Visual Basic Editor.
- On the Tools menu, click References.
Note To add the reference using Microsoft Visual Basic 6.0, click References on the Project menu.
- 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:
- For Visio 2007, click to select the Microsoft Visio 12.0 Type Library check box.
- For Visio 2003, click to select the Microsoft Visio 11.0 Type Library check box.
- For Visio 2002, click to select the Microsoft Visio 2002 Type Library check box.
back to the top
Step 2: Declare the object variableTo 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 AppsThe following sample Visual Basic argument declares the variable AppVisio as an object of type Visio.Application:
back to the top
Dim AppVisio as Visio.Application
Step 3: Set the variableYou can use the following two Visual Basic methods to activate Visio:
The following sample argument sets the AppVisio variable to the Visio application using the CreateObject function:
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 Set AppVisio = CreateObject("Visio.Application")
Note You can also use the CreateObject function to create a Visio instance that is invisible. For example:
Dim AppVisio As Visio.Application On Error Resume Next Set AppVisio = GetObject(, "visio.application") If AppVisio Is Nothing Then Set AppVisio = CreateObject("visio.application") End If
You can then use the Application object's Visible property to control whether the instance is visible.
Set AppVisio = CreateObject("Visio.InvisibleApp")
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 propertiesAfter 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.
back to the top
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("visio.application") Set docsObj = AppVisio.Documents ' Create a document based on the Basic Diagram template that ' automatically opens the Basic Shapes stencil. Set DocObj = docsObj.Add("Basic Diagram.vst") 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("Basic Shapes.vss") Set mastObj = stnObj.Masters("Rectangle") ' 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 = "This is some text." ' Save the drawing and quit Visio. The message pauses the program ' so you can see the Visio drawing before the instance closes. DocObj.SaveAs "MyDrawing.vsd" MsgBox "Drawing finished!", , "AutoVisio (OLE) Example" ' Quit Visio. AppVisio.Quit ' Clear the variable from memory. Set AppVisio = NothingEnd Sub
Microsoft Visio Developer Web sitesFor more information about how to automate Visio, visit the following Microsoft Web site:
Visio Software Development KitsTo download the Visio 2002 Software Development Kit, visit the following Microsoft Web site:
To download the Visio 2003 Software Development Kit, visit the following Microsoft Web site:
NewsGroupsThe following peer-to-peer newsgroup is available to help you interact with other users of Visual Basic for Applications:
Visual Basic HelpFor 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
vb vba vbe OLE automation macro GetObject CreateObject sample example vso2002 vso2003 vso2007
Article ID: 309603 - Last Review: 04/11/2007 23:33:40 - Revision: 4.1
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
- kbautomation kbprogramming kbvba kbhowto KB309603