Hey, Scripting Guy! Is It Possible to Automate Microsoft Visio?


 Bookmark and Share


Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a question. You guys seem to have a lot of material about automating Microsoft Office Word and Microsoft Office Excel. You have even written some stuff about automating Microsoft Office Outlook, but I have never seen anything about automating Microsoft Office Visio. I am going to assume that it can be done, because one of the great things about Microsoft Office is its customization and automation model. I have searched the Internet and have not come up with anything. Is it even possible to automate Microsoft Visio? Is it possible? Is it too hard? What’s the deal?

— LF


Hey, Scripting Guy! Answer Hello LF,

Microsoft Scripting Guy Ed Wilson here. I love Charlotte, North Carolina, in January. It is a cool 50 degrees Fahrenheit outside (10 degrees Celsius) and sunny. If I played golf, it would be perfect weather for a nice 18 holes, but I do not play golf—I write scripts. Golf can be an expensive hobby; writing a script is free. The way I play golf, writing scripts is much more enjoyable. Therefore, 50 degrees Fahrenheit and sunny is a perfect day to play around and write some cool Windows PowerShell scripts. First, I munch ANZAC Biscuits and listen to AC/DC.

One thing you will discover when automating Microsoft Visio is that the object model is similar to the object model for Microsoft Word or Microsoft Excel. Though you cannot follow a Microsoft Word script and use it as a pattern to automate Microsoft Visio, if you are familiar with automating Microsoft Word the process involved with Microsoft Visio will not be completely new to you.

When automating Microsoft Visio, the first thing you need to do is to create an instance of the application object. Because it is a COM object, you use the New-Object cmdlet with the –comobject parameter and specify the program ID of Visio.Application. The application object is documented on MSDN and provides access to the Visio application. Store the returned application object in the $application variable:

$application = New-Object -ComObject Visio.Application

You query the documents property of the application object to return a documents collection object. The documents object includes a Document object for each open document in a Microsoft Office Visio instance. It contains a number of methods for working with documents. Store the documents object in the $documents variable:

$documents = $application.Documents

Next use the add method from the documents object to create a new document. Pass the name of the template to the add method to allow you to base the new drawing on an existing Visio template. If you want to create a new drawing that is not based upon an existing Visio template, you use empty quotation marks:

$document = $documents.Add(“”)

Keep in mind that most of the time, you will want to base your new drawing on one of the existing Visio templates because they set a number of defaults that are useful and will save you a lot of effort. The names of the templates are pretty much the same as the ones seen in the New file menu seen here.

Image of Microsoft Visio New file menu

To add a new Visio drawing based upon the Basic Diagram template, use the add method. Store the returned document object in the $document variable:

$document = $documents.Add(“Basic Diagram.vst”)

After you have a document object, use the pages property from the ActiveDocument property to return the document object and the pages property from the document object to return a pages collection object. Store t he pages object in the $pages variable:

$pages = $application.ActiveDocument.Pages

The default property from the pages object is the item property. Use the item property to return the first page in the page collection. Store the returned page object in the $page variable:

$page = $pages.Item(1)

Because the Basic Diagram template that we used to create the new drawing includes the Basic Shapes stencil as seen in the following image, you might assume that the Basic Shapes stencil would automatically be available. However, this is not the case. You must add any shape sheet you wish to use before accessing the shapes it contains.

Image of Basic Diagram template that includes Basic Shapes stencil

To add a shape sheet, use the add method from the documents collection, and specify the name of the shape sheet with the .vss extension. Store the returned document object in a variable named $stencil for ease of identification:

$stencil = $application.Documents.Add(“Basic Shapes.vss”)

After you have added the shape sheet to the documents collection, use the masters property from the document object to return the masters collection object for the documents stencil. Use the item property to retrieve a specific item from the stencil.

Store the returned master object in a variable named $item. When retrieving a master object from a stencil, the name you will use follows the names you see in the stencil, as seen in the following image.

Image of names of stencils

To add a square, use the code seen here.

$item = $stencil.Masters.Item(“Square”)

It is time to place the symbol on the page. To do this use the drop method from the page object. You need to specify the master object stored in the $item variable, as well as an x/y coordinate. Store the returned shape object in the $shape variable:

$shape = $page.Drop($item, 1.0, 10.6)

It is time to add some text to the shape. To do this, use the text property of the shape object:

$shape.Text = “This is some text.”

To save the drawing, use the saveas method from the document object and specify the path and name of the drawing, as seen here:


Close Visio by using the quit method from the application object, as shown here:


After the script has run, the Microsoft Visio drawing seen in the following image is saved in the location you specified.

Image of Microsoft Visio drawing created when script is run


LF, that is all there is to using Windows PowerShell to create a Visio drawing. The script for today can also be found at the Script Repository.  Microsoft Visio Week will continue tomorrow.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.


Ed Wilson and Craig Liebendorfer, Scripting Guys



Discussion is closed.

Feedback usabilla icon