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

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

  • Comments 6
  • Likes

 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 the 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:

$document.SaveAs("C:\fso\MyDrawing.vsd")

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

$application.Quit()

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

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Microsoft Visio Add-in such as the Disk Space Monitoring tool employs the radical Data Graphics feature of Visio 2007 to display the results of server monitoring. Another add in is the Visio for SQL Server Add-In, a tool for IT administrators who require to continually interact with users during SQL server installations in any IT infrastructure.

  • Hi Scripting Guy!

    I've a question about creating connectors to the right shapes.

    I'm creating Visio Shapes in an foreach loop. So everytime a new shape is being created the next statement is being used:

    $shape = $page.Drop($item, $horizontal, $vertical)

    But now I want to change the $shape variable to a something different for each object in the foreach loop.

    I tried to use the Invoke-Expression. Like this: Invoke-Expression ('$Shape' + "$a" + '='+"$page.Drop($item,$Horizontal,$Vertical)")

    But that's not working. Any ideas how to solve this?

    Regards,

    Stefan

  • Hi Scripting Guys,

    I found a solution. When creating the Visio Shapes I also add a text shape.

    So I used that to find the correct Visio Shapes when creating the connectors.

    Example:

    $ShapeConnector = $page.Shapes | where {$_.Text -eq $server}

    $ShapeConnector.AutoConnect($Shape, 0, $connector)

    Cool it worked!

    Regards,

    Stefan

  • Hi scripting guy,

    I like to add a new page to visio via powershell.

    I can't seem to find how this work.

    I have tried $page.new, $page.add it does not work. do you have any sugestions

    regards

    jeroen van de pol

  • Dear Scripting Guy,

    I am new to visio scripting but familiar with VBA - I am looking for a script to use in an Org-Chart that is linked to Access DB; so when one employee leave the company and I Refresh the link, it would take me to the page and highlight the object to delete - can you help me, Thanks!

  • Hi Visio GUY.  What is a good resource to getting started with Visio scripting..background is some C+, some sQL..but not advance.  I'm trying to create sales tools...Kind REGARDS.  I can be emailed at dallas20102010    at   gmail...thanks.