Article ID: 309603 - View products that this article applies to.
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.
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:
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:
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:
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:
138138The following sample Visual Basic argument declares the variable AppVisio as an object of type Visio.Application:
(http://support.microsoft.com/kb/138138/ )Late, ID, Early Binding types possible in VB for Apps
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:
Note You can also use the CreateObject function to create a Visio instance that is invisible. For example:
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.
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.
Microsoft Visio Developer Web sitesFor more information about how to automate Visio, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/office/aa905478.aspxFor more information about how to develop Microsoft Visio solutions, 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.
Article ID: 309603 - Last Review: April 11, 2007 - Revision: 4.1