{"id":51593,"date":"2010-01-11T00:01:00","date_gmt":"2010-01-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/01\/11\/hey-scripting-guy-is-it-possible-to-automate-microsoft-visio\/"},"modified":"2010-01-11T00:01:00","modified_gmt":"2010-01-11T00:01:00","slug":"hey-scripting-guy-is-it-possible-to-automate-microsoft-visio","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-is-it-possible-to-automate-microsoft-visio\/","title":{"rendered":"Hey, Scripting Guy! Is It Possible to Automate Microsoft Visio?"},"content":{"rendered":"<p class=\"MsoNormal\">&nbsp;<a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><font size=\"2\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font><\/p>\n<p class=\"MsoNormal\">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&rsquo;s the deal?<\/p>\n<p class=\"MsoNormal\">&#8212; LF<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">&nbsp;Hello LF, <\/p>\n<p class=\"MsoNormal\">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&mdash;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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Anzac_biscuits\"><font face=\"Segoe\">ANZAC Biscuits<\/font><\/a> and listen to AC\/DC. <\/p>\n<p class=\"MsoNormal\">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. <\/p>\n<p class=\"MsoNormal\">When automating Microsoft Visio, the first thing you need to do is to create an instance of the <b>application<\/b> object. Because it is a COM object, you use the <b>New-Object<\/b> cmdlet with the <b>&ndash;comobject<\/b> parameter and specify the program ID of <b>Visio.Application<\/b>. The <b>application<\/b> object is documented on <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa730953.aspx\"><font face=\"Segoe\">MSDN<\/font><\/a> and provides access to the Visio application. Store the returned <b>application<\/b> object in the <b>$application<\/b> variable: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$application = New-Object -ComObject Visio.Application<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You query the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms426010.aspx\"><font face=\"Segoe\">documents property<\/font><\/a> of the <b>application<\/b> object to return a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa731003.aspx\"><font face=\"Segoe\">documents collection object<\/font><\/a>. The documents object includes a <b>Document<\/b> object for each open document in a Microsoft Office Visio instance. It contains a number of methods for working with documents. Store the <b>documents<\/b> object in the <b>$documents<\/b> variable:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$documents = $application.Documents<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Next use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms425621.aspx\"><font face=\"Segoe\">add method<\/font><\/a> from the <b>documents<\/b> object to create a new document. Pass the name of the template to the <b>add<\/b> 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:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$document = $documents.Add(&#8220;&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">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 <b>New<\/b> file menu seen here.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Microsoft Visio New file menu\" alt=\"Image of Microsoft Visio New file menu\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0111\/hsg-01-11-10-01.jpg\" width=\"295\" height=\"187\"><\/p>\n<p class=\"MsoNormal\">To add a new Visio drawing based upon the Basic Diagram template, use the <b>add<\/b> method. Store the returned <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa730998.aspx\"><font face=\"Segoe\">document object<\/font><\/a> in the <b>$document<\/b> variable: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$document = $documents.Add(&#8220;Basic Diagram.vst&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have a <b>document<\/b> object, use the <b>pages<\/b> property from the <b>ActiveDocument<\/b> property to return the <b>document<\/b> object and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms196190.aspx\"><font face=\"Segoe\">pages property<\/font><\/a> from the <b>document<\/b> object to return <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms368295.aspx\"><font face=\"Segoe\">a pages collection object<\/font><\/a>. Store t\nhe <b>pages<\/b> object in the <b>$pages<\/b> variable: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$pages = $application.ActiveDocument.Pages<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>default<\/b> property from the <b>pages<\/b> object is the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms426105.aspx\"><font face=\"Segoe\">item property<\/font><\/a>. Use the <b>item<\/b> property to return the first page in the <b>page<\/b> collection. Store the returned <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms408981.aspx\"><font face=\"Segoe\">page object<\/font><\/a> in the <b>$page<\/b> variable:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$page = $pages.Item(1)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">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. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Basic Diagram template that includes Basic Shapes stencil\" alt=\"Image of Basic Diagram template that includes Basic Shapes stencil\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0111\/hsg-01-11-10-02.jpg\" width=\"600\" height=\"420\"><\/p>\n<p class=\"MsoNormal\">To add a shape sheet, use the <b>add<\/b> method from the <b>documents<\/b> collection, and specify the name of the shape sheet with the <b>.vss<\/b> extension. Store the returned <b>document<\/b> object in a variable named <b>$stencil<\/b> for ease of identification:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$stencil = $application.Documents.Add(&#8220;Basic Shapes.vss&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have added the shape sheet to the documents collection, use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms196131.aspx\"><font face=\"Segoe\">masters property<\/font><\/a> from the <b>document<\/b> object to return the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa342262.aspx\"><font face=\"Segoe\">masters collection object<\/font><\/a> for the documents stencil. Use the <b>item<\/b> property to retrieve a specific item from the stencil. <\/p>\n<p class=\"MsoNormal\">Store the returned <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa731055.aspx\"><font face=\"Segoe\">master object<\/font><\/a> in a variable named <b>$item<\/b>. 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.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of names of stencils\" alt=\"Image of names of stencils\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0111\/hsg-01-11-10-03.jpg\" width=\"172\" height=\"289\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0111\/hsg-01-11-10-03.jpg\"><font face=\"Segoe\"><\/font><\/a><\/p>\n<p>To add a square, use the code seen here. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$item = $stencil.Masters.Item(&#8220;Square&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is time to place the symbol on the page. To do this use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms195970.aspx\"><font face=\"Segoe\">drop method<\/font><\/a> from the <b>page<\/b> object. You need to specify the master object stored in the <b>$item<\/b> variable, as well as an x\/y coordinate. Store the returned <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms408994.aspx\"><font face=\"Segoe\">shape object<\/font><\/a> in the <b>$shape<\/b> variable:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape = $page.Drop($item, 1.0, 10.6) <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is time to add some text to the shape. To do this, use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms426549.aspx\"><font face=\"Segoe\">text property<\/font><\/a> of the <b>shape<\/b> object: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape.Text = &#8220;This is some text.&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To save the drawing, use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms427932.aspx\"><font face=\"Segoe\">saveas method<\/font><\/a> from the <b>document<\/b> object and specify the path and name of the drawing, as seen here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$document.SaveAs(&#8220;C:fsoMyDrawing.vsd&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Close Visio by using the <b>quit<\/b> method from the <b>application<\/b> object, as shown here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$application.Quit()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the script has run, the Microsoft Visio drawing seen in the following image is saved in the location you specified.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Microsoft Visio drawing created when script is run\" alt=\"Image of Microsoft Visio drawing created when script is run\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0111\/hsg-01-11-10-04.jpg\" width=\"600\" height=\"420\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">LF, that is all there is to using Windows PowerShell to create a Visio drawing.<span>&nbsp;The script for today can also be found at the <a href=\"http:\/\/bit.ly\/5orpb8\">Script Repository<\/a>. &nbsp;<\/span>Microsoft Visio Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[53,49,3,45],"class_list":["post-51593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-visio","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51593"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51593\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=51593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}