{"id":51583,"date":"2010-01-12T00:01:00","date_gmt":"2010-01-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/01\/12\/hey-scripting-guy-can-i-make-a-microsoft-visio-drawing-with-network-shapes-and-symbols\/"},"modified":"2010-01-12T00:01:00","modified_gmt":"2010-01-12T00:01:00","slug":"hey-scripting-guy-can-i-make-a-microsoft-visio-drawing-with-network-shapes-and-symbols","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-make-a-microsoft-visio-drawing-with-network-shapes-and-symbols\/","title":{"rendered":"Hey, Scripting Guy! Can I Make a Microsoft Visio Drawing with Network Shapes and Symbols?"},"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 been excited about the Microsoft Visio Week articles since I heard your interview back during the holidays. When I read your <a href=\"http:\/\/bit.ly\/scriptingguystwitter\"><font face=\"Segoe\">tweets<\/font><\/a> as you were the writing the Hey, Scripting Guy! posts, I really got excited. I must say that reading yesterday&rsquo;s article where you added a square to a Microsoft Visio drawing was cool but somewhat of a letdown. I am a network administrator and want to be able to use Microsoft Visio to do network drawings and stuff like that. I imagine my six-year-old daughter would enjoy running your draw-a-square script, but even she would get bored before too long. How about showing us network shapes and symbols? <\/p>\n<p class=\"MsoNormal\">&#8212; CF<\/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\">Hello CF, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson. I just realized something. I have no tea, no music playing, and there is no sun outside. It is as if the weather outside is mirroring my poor dismal existence. Wait a second, please. Okay, I am back. I am using my new tea pot the Scripting Wife got for me to brew a pot of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Rooibos\">rooibos tea<\/a>. I fell in love with rooibos tea in Hamburg when I was teaching a Windows PowerShell workshop. I took the picture of the town hall (Rathaus) that is seen just below from a little sidewalk caf&eacute; that served awesome rooibos tea. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Rathaus Hamburg\" alt=\"Image of Rathaus Hamburg\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0112\/hsg-01-12-10-06.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">CF, we need some music. I am dying to listen to my new <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bruce_Hornsby\"><font face=\"Segoe\">Bruce Hornsby<\/font><\/a> disk that the Scripting Wife gave me. Music, tea<span>&mdash;<\/span>we are all set. Let&rsquo;s get to work.<\/p>\n<p class=\"MsoNormal\">The first thing we need to do when automating Microsoft Visio is to create an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa730953.aspx\">application object<\/a>. The <b>application<\/b> object is the main object for working with Microsoft Visio and controls the way the application runs. Use the <b>New-Object<\/b> cmdlet with the <b>&ndash;comobject<\/b> parameter to create an instance of the <b>visio.application<\/b> object. Store the returned application object in the <b>$application<\/b> variable, as seen here:<\/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\">The <b>application<\/b> object has the <b>visible<\/b> property that accepts a Boolean value. It is used to govern whether the application will be visible while it is running. Because we are doing everything programmatically, there is no reason to waste resources watching the drawing unfold. Besides, on my computer it happens so fast, it is not much of a show. You will see a flicker as the <b>application<\/b> object is created, and then the <b>visible<\/b> property is set to false. After that you will not know if the script has completed until you see the return code in either the Windows PowerShell console or the Windows PowerShell ISE. Setting the <b>visible<\/b> property to false makes the application invisible. Setting it to true, will cause it to be displayed on the screen. The line of code that controls this behavior is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$application.visible = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The next several lines of code are basically the same as were seen yesterday. Refer to <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/01\/11\/hey-scripting-guy-january-11-2010.aspx\">yesterday&rsquo;s Hey, Scripting Guy! post<\/a> for more information about this. Because we are going to make a network diagram, we choose to use the Basic Network Diagram.vst template. This name is obtained by clicking <b>New<\/b> in the <b>File<\/b> menu and choosing the type of drawing to create. This is seen here.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of New file menu\" alt=\"Image of New file menu\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0112\/hsg-01-12-10-01.jpg\" width=\"266\" height=\"325\"><\/p>\n<p class=\"MsoNormal\">The code that creates the <b>document<\/b> object, adds the template, and adds a new page to the <b>pages<\/b> collection is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$documents = $application.Documents<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$document = $documents.Add(&#8220;Basic Network Diagram.vst&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$pages = $application.ActiveDocument.Pages<\/p>\n<p><\/font><\/span><\/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\">N\now we run into our first problem. In <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/01\/11\/hey-scripting-guy-january-11-2010.aspx\">yesterday&rsquo;s Hey, Scripting Guy! post<\/a>, I stated that to add a stencil, you could use the name of the stencil that was displayed in the <b>Shapes<\/b> section of Microsoft Visio. The <strong>Shapes<\/strong> section is seen in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of the Visio Shapes section\" alt=\"Image of the Visio Shapes section\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0112\/hsg-01-12-10-02.jpg\" width=\"189\" height=\"371\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">This technique works for many of the stencils. You just need to remember to add the .vss extension to the name. The problem came when I wanted to add the <b>Network and Peripherals<\/b> stencil to the drawing. After spending nearly an hour trying various permutations and ensuring I had the word &ldquo;peripheral&rdquo; spelled correctly, I was about to give up. I searched MSDN for a list of included stencil names, as well as the Microsoft Visio online Help to no avail. But it is really hard to do a network diagram without using some of the network and peripheral shapes. Then I remembered an old trick I had written about in <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/dd793612.aspx\">one of my VBScript books<\/a>&mdash;use the macro recorder. Even though the code will be completely bogus, the name of the object being added will be correct. I quickly opened Microsoft Visio and recorded a macro that added the <b>Network and Peripherals (Metric)<\/b> stencil. I then opened the macro editor by clicking <b>Macro<\/b> in the <b>Tools<\/b> menu, clicking <b>Macro<\/b>, choosing the newly recorded macro (macro1 by default), and clicking <b>Edit<\/b>. The Microsoft Visual Basic Macro editor appears. As seen in the following image, the stencil sheet is named &ldquo;periph_m.vss&rdquo;<span>&mdash;<\/span>something I would never have guessed.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of the name of stencil sheet\" alt=\"Image of the name of stencil sheet\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0112\/hsg-01-12-10-03.jpg\" width=\"600\" height=\"420\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">As I mentioned earlier, the VBA code won&rsquo;t work for our purposes, but because the only thing I did was add the stencil, it is a pretty safe bet we have found the secret name of our stencil. Use the <b>add<\/b> method from the <b>documents<\/b> object to add the desired stencil, and store the returned <b>document<\/b> object in the <b>$NetworkStencil<\/b> variable:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$NetworkStencil = $application.Documents.Add(&#8220;periph_m.vss&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">We add the other two stencils by using the display names without further incident. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$ComputerStencil = $application.Documents.Add(&#8220;Computers and Monitors.vss&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$ConnectorStencil = $application.Documents.Add(&#8220;Connectors.vss&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Next we use the same procedure we used yesterday to add four computers and an ethernet network to the Microsoft Visio drawing. Because the Basic Network Diagram.vst template is in landscape orientation instead of portrait orientation, the numbers we use for placing the shapes on the drawing will need to change. One other thing to keep in mind is that the size and shape of the shapes will dictate the numbers you choose for placement as well. If you keep in mind the size and shape of an 8.5-by-11-inch sheet of paper, the numbers make a little sense. This is not a strict measurement, but will give you a feel for the numbers. The numbers are x\/y coordinates. To add a second line to the <b>text<\/b> property of the shape, use the &ldquo;<b>`r`n<\/b>&rdquo; characters that Windows PowerShell recognizes as the line-feed sequence. The <b>PC<\/b> shape comes from the <b>Computers and Monitors<\/b> stencil, as seen in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of PC shape from Computers and Monitors stencil\" alt=\"Image of PC shape from Computers and Monitors stencil\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0112\/hsg-01-12-10-04.jpg\" width=\"189\" height=\"350\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">To retrieve the <b>PC<\/b> shape, use the <b>Item<\/b> method from the <b>masters<\/b> collection of the stencil stored in the <b>$computerStencil<\/b> variable. The code to add a PC shape to the lower right hand side of the drawing is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$pc = $ComputerStencil.Masters.Item(&#8220;PC&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape1 = $page.Drop($pc, 11.0, 1.0) <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape1.Text = &#8220;Lower Right`r`nShape1&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Next add the other three <b>PC<\/b> shapes to the three remaining corners of the Microsoft Visio drawing. Use the same technique that was used to add shape1: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape2 = $page.Drop($pc, 2.2, 1.0) <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape2.Text = &#8220;Lower Left`r`nShape2&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape3 = $page.Drop($pc, 11.0, 6.8) <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape3.Text = &#8220;Upper Right`r`nShape3&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape4 = $page.Drop($pc, 2.2, 6.8) <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape4.Text = &#8220;Upper Left`r`nShape4&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is time to add the <b>Ethernet<\/b> shape. The <b>Ethernet<\/b> shape is contained in the <b>Network and Peripherals<\/b> stencil, which is stored in the <b>$networkStencil<\/b> variable. Use the same technique to create an instance of the <b>Ethernet<\/b> shape and add it to the drawing that was used to add the <b>PC<\/b> shapes to the drawing. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$etherNet = $NetworkStencil.Masters.Item(&#8220;Ethernet&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape5 = $page.Drop($etherNet,6.0,4.2)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$shape5.Text = &#8220;center&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To connect the shapes, we need to add a connector. The dynamic connector is fun to use and is contained on the <b>Connectors<\/b> stencil, which we have stored in the <b>$connectorStencil<\/b> variable. Use the <b>item<\/b> method to retrieve an instance of the <b>Dynamic Connector<\/b> shape and store the returned shape in the <b>$connector<\/b> variable as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$connector = $ConnectorStencil.Masters.item(&#8220;Dynamic Connector&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When you have created a connector shape, use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms427221.aspx\"><font face=\"Segoe\">AutoConnect method<\/font><\/a> from the <b>shape<\/b> object to connect the <b>PCs<\/b> and the <b>Ethernet<\/b> shapes to each other. The <b>AutoConnect<\/b> method takes three parameters: the shape to connect, the direction of the connection, and the connector to use. In our script we are connecting to the <b>Ethernet<\/b> shape that is stored in the <b>$shape5<\/b> variable, and we are using the dynamic connector that is stored in the <b>$connector<\/b> variable. The enumeration value 0 means to connect without relocating the shapes. Other enumeration values for the <b>VisAutoConnectDir<\/b> enumeration are seen in Table 1. <\/p>\n<p class=\"MsoNormal\"><b>Table 1<span>&nbsp; <\/span>Enumeration Values of the VisAutoConnectDir Enumeration<\/p>\n<p><\/b><\/p>\n<table class=\"MsoNormalTable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"182\">\n<div>\n<p class=\"TableText\"><b>Constant<\/p>\n<p><\/b><\/p>\n<\/div>\n<\/td>\n<td valign=\"top\" width=\"189\">\n<div>\n<p class=\"TableText\"><b>Value<\/p>\n<p><\/b><\/p>\n<\/div>\n<\/td>\n<td width=\"176\">\n<div>\n<p class=\"TableText\"><b>Description <\/p>\n<p><\/b><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"182\">\n<div>\n<p class=\"TableText\">visAutoConnectDirDown<\/p>\n<\/div>\n<\/td>\n<td valign=\"top\" width=\"189\">\n<div>\n<p class=\"TableText\">2<\/p>\n<\/div>\n<\/td>\n<td valign=\"top\" width=\"176\">\n<div>\n<p class=\"TableText\">Connect down.<\/p>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"182\">\n<div>\n<p class=\"TableText\">visAutoConnectDirLeft<\/p>\n<\/div>\n<\/td>\n<td valign=\"top\" width=\"189\">\n<div>\n<p class=\"TableText\">3<\/p>\n<\/div>\n<\/td>\n<td valign=\"top\" width=\"176\">\n<div>\n<p class=\"TableText\">Connect to the left.<\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I have been excited about the Microsoft Visio Week articles since I heard your interview back during the holidays. When I read your tweets as you were the writing the Hey, Scripting Guy! posts, I really got excited. I must say that reading yesterday&rsquo;s article where you added a square [&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-51583","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 been excited about the Microsoft Visio Week articles since I heard your interview back during the holidays. When I read your tweets as you were the writing the Hey, Scripting Guy! posts, I really got excited. I must say that reading yesterday&rsquo;s article where you added a square [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51583","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=51583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51583\/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=51583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}