Microsoft KB Archive/305199

= How to automate Visio with Visual Basic .NET =

Article ID: 305199

Article Last Modified on 4/16/2007

-

APPLIES TO


 * Microsoft Office Visio Professional 2007
 * Microsoft Office Visio Professional 2003
 * Microsoft Visio 2002 Professional Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q305199



SUMMARY
This article demonstrates how to automate Visio using Visual Basic .NET.



MORE INFORMATION
The sample automation code in this article does the following:
 * Draws a simple flowchart on the Visio drawing page that is based on data that is contained in an array.
 * Names the page.
 * Saves the document.

The array is two-dimensional: The first element contains the name of the shape, and the second element contains the text of the shape.

Steps to Create the Sample Visual Basic .NET Application
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual Basic Projects, then click Windows Application under Templates. Form1 is created by default. Add a reference to the Visio object library. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, click one of the following options, and click Select:  For Visio 2007, click Microsoft Visio 12.0 Type Library. For Visio 2003, click Microsoft Visio 11.0 Type Library. For Visio 2002, click Microsoft Visio 2002 Type Library.</li></ul>

Note If you have not already done so, Microsoft recommends that you download and install the Microsoft Office XP Primary Interop Assemblies (PIAs).

For more information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:

328912 Microsoft Office XP primary interop assemblies (PIAs) are available for download

</li> Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li> On the View menu, click ToolBox. Add a button to Form1.</li> Double-click Button1. The code window opens at the Click event for Button1.</li>  In the code window, replace the following code Private Sub Button1_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles Button1.Click End Sub with: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As     System.EventArgs) Handles Button1.Click fBuildArray fBuildFlowchart End Sub </li> On the Project menu, click Add Module.</li>  Replace the contents of Module1 with the following: Module Module1 Public aryValues(5, 2) As String Sub fBuildArray aryValues(0, 0) = &quot;Terminator&quot; aryValues(1, 0) = &quot;Document&quot; aryValues(2, 0) = &quot;Decision&quot; aryValues(3, 0) = &quot;Process&quot; aryValues(4, 0) = &quot;Terminator&quot;

aryValues(0, 1) = &quot;Begin Routing Process&quot; aryValues(1, 1) = &quot;Gather Customer Data&quot; aryValues(2, 1) = &quot;Supported Issue?&quot; aryValues(3, 1) = &quot;Dispatch to Queue&quot; aryValues(4, 1) = &quot;End Routing Process&quot; End Sub

Sub fBuildFlowchart Dim vApp As Visio.Application Dim vDoc As Visio.Document Dim vFromShape As Visio.Shape Dim vToShape As Visio.Shape Dim vConnector As Visio.Shape Dim vFlowChartMaster As Visio.Master Dim vConnectorMaster As Visio.Master Dim vStencil As Visio.Document Dim dblXLocation As Double Dim dblYLocation As Double Dim vBeginCell As Visio.Cell Dim vEndCell As Visio.Cell Dim iCount As Integer Const TEMPLATEPATH = &quot;C:\Program Files\Microsoft Office\Visio10\1033\&quot; _ & &quot;Solutions\Flowchart\Basic Flowchart Shapes (US units).vss&quot;

' Change this constant to match your choice of location and file name. Const SAVENEWFILE = &quot;C:\Simpleflowchart.vsd&quot;

' Start point measured from the bottom left corner. dblXLocation = 4.25 dblYLocation = 8.5

vApp = New Visio.Application 'Create a new document; note the empty string. vDoc = vApp.Documents.Add(&quot;&quot;) vStencil = vApp.Documents.OpenEx(TEMPLATEPATH, 4)

For iCount = LBound(aryValues) To UBound(aryValues) - 1 vFlowChartMaster = vStencil.Masters(aryValues(iCount, 0)) vToShape = vApp.ActivePage.Drop(vFlowChartMaster, _           dblXLocation, dblYLocation) vToShape.Text = aryValues(iCount, 1) If Not vFromShape Is Nothing Then If vConnectorMaster Is Nothing Then vConnectorMaster = vStencil.Masters(&quot;Dynamic Connector&quot;) End If           vConnector = vApp.ActivePage.Drop(vConnectorMaster, 0, 0) vBeginCell = vConnector.Cells(&quot;BeginX&quot;) vBeginCell.GlueTo(vFromShape.Cells(&quot;AlignBottom&quot;)) vEndCell = vConnector.Cells(&quot;EndX&quot;) vEndCell.GlueTo(vToShape.Cells(&quot;AlignTop&quot;)) vConnector.SendToBack End If        vFromShape = vToShape vToShape = Nothing dblYLocation = dblYLocation - 1.5 Next vDoc.Pages(1).Name = &quot;Flowchart Example&quot; Try ' Delete the previous version of the file. Kill(SAVENEWFILE) Catch End Try vDoc.SaveAs(SAVENEWFILE) vDoc.Close vApp.Quit vDoc = Nothing vApp = Nothing GC.Collect End Sub End Module Note Modify the TEMPLATEPATH constant to point to your Visio installation folder. By default, the location is one of the following: <ul> For Office Visio 2007, the default location is C:\Program Files\Microsoft Office\Visio12\1033\BASFLO_U.VSS.</li> For Visio 2003, the default location for this file is C:\Program Files\Microsoft Office\Visio11\1033\BASFLO_U.VSS</li> For Visio 2002, the default location for this file is C:\Program Files\Microsoft Visio\Visio10\1033\Solutions\Flowchart\Basic Flowchart Shapes (US units).vss.</li></ul> </li> On the Debug menu, click Start.</li> Open C:\Simpleflowchart.vsd in Visio to see the results.</li></ol>

<div class="references_section">