{"id":53783,"date":"2009-05-11T23:21:00","date_gmt":"2009-05-11T23:21:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/05\/11\/hey-scripting-guy-how-can-i-convert-a-vbscript-to-a-windows-powershell-script-that-creates-an-office-word-document-with-hyperlinks\/"},"modified":"2009-05-11T23:21:00","modified_gmt":"2009-05-11T23:21:00","slug":"hey-scripting-guy-how-can-i-convert-a-vbscript-to-a-windows-powershell-script-that-creates-an-office-word-document-with-hyperlinks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-convert-a-vbscript-to-a-windows-powershell-script-that-creates-an-office-word-document-with-hyperlinks\/","title":{"rendered":"Hey, Scripting Guy! How Can I Convert a VBScript to a Windows PowerShell Script That Creates an Office Word Document with Hyperlinks?"},"content":{"rendered":"<h2><img decoding=\"async\" class=\"nearGraphic\" 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\"> Hey, Scripting Guy! I am trying to learn Windows PowerShell. I have a script that creates a Word document that has a series of hyperlinks in it. I want to be able to write that script in Windows PowerShell, but I am not sure where I should begin. As far as I can tell, there are no Windows PowerShell cmdlets that will let me do this. Am I out of luck?<\/p>\n<p>&#8211; MF<br><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" class=\"nearGraphic\" 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\"> <\/p>\n<p>Hi MF,<\/p>\n<p>It is a shame there is no <b>New-WordDoc<\/b> cmdlet. It would definitely simplify things significantly. I am glad you said you are trying to learn Windows PowerShell, because in most cases I do not recommend that people just go around converting VBScript to Windows PowerShell. For one thing, you can do so much more with Windows PowerShell than you can do with VBScript that I have yet to run out of ideas for new Windows PowerShell scripts. I have written more than 3,000 VBScripts in my life, and of those, I have translated fewer than 300 into Windows PowerShell. For one thing, if I spend an hour converting a VBScript to a Windows PowerShell script, what do I have at the end of that hour? I have the same thing I had before, only now it is Windows PowerShell instead of VBScript. That&rsquo;s fine, but I have achieved no new functionality, and I am doing nothing new. <\/p>\n<p>As an example, I have a script that I use when I travel. It turns off my wireless network adapter, turns on my ethernet card, and shuts down the laptop. I use this when I am in an office with wireless connectivity and staying in a hotel that uses Cat 5. It saves me time. The script also reverses the process. Therefore, I am ready to go in the morning when I go back to the office. I have felt no need to translate it to Windows PowerShell. You will find <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept08\/hey0929.mspx\" target=\"_blank\">a similar version of this script<\/a>, but it&rsquo;s written in Windows PowerShell. Keeping these tips in mind should help you remain calm, serene, and happy, like this little fella I saw a couple of years ago while scuba diving off Kona on the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Big_Island_of_Hawaii\" target=\"_blank\">Island of Hawaii<\/a> in the United States.<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of a seemingly happy eel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0511\/hsg-05-11-09-01.jpg\" width=\"500\" height=\"375\"><\/p>\n<p>&nbsp;<\/p>\n<table id=\"EJD\" class=\"dataTable\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">This week we are looking at how to migrate VBScript to Windows PowerShell. You should definitely check out the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/convert\/default.mspx\" target=\"_blank\">VBScript-to-Windows PowerShell Conversion Guide<\/a>. This is included as Appendix C in the Microsoft Press book, <a href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth10329.aspx\" target=\"_blank\">Microsoft Windows PowerShell Step by Step<\/a>. It is also in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/pschm.mspx\" target=\"_blank\">Windows PowerShell Graphical Help File<\/a>. Clearly, we are proud of that thing. You may also want to check out our <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" target=\"_blank\">Windows PowerShell Scripting Hub<\/a> where you will find links to the Windows PowerShell Owner&#8217;s Manual (very popular!) and other resources that will help you to convert VBScript to Windows PowerShell. One additional book that would be useful is the Microsoft Press book, <a href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth9541.aspx\" target=\"_blank\">Windows PowerShell Scripting Guide<\/a>. This book is useful if you are working with WMI, or if you are trying to go beyond simple line-by-line translations of one script to another.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>MF, there is a <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb05\/hey0215.mspx\" target=\"_blank\">Hey, Scripting Guy!<\/a> article that was published back in February 2005 that talks about adding hyperlinks to a Office Word document. Because we will not be covering the VBScript version of that script, refer to that article for detailed information about how the script works. For ease of learning, however, we took the liberty of pasting that script here:<\/p>\n<p><b>AddHyperlinkToWord.vbs<\/b><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)<br>objWord.Visible = TRUE<br>Set objDoc = objWord.Documents.Add()<br>Set objRange = objDoc.Range()<br>Set objLink = objDoc.Hyperlinks.Add _<br><span>&nbsp;&nbsp;&nbsp; <\/span>(objRange, &#8221; http:\/\/www.microsoft.com\/technet\/scriptcenter &#8220;, , , &#8220;Script Center&#8221;)<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p>When the <b>AddHyperlinkToWord.vbs<\/b> script runs, it opens Word and adds the words &#8220;Script Center&#8221; on a new line. Then it turns the words &ldquo;Script Center&rdquo; into a hyperlink that points to http:\/\/www.microsoft.com\/technet\/scriptcenter:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of the output from the AddHyperLinkToWord.vbs script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0511\/hsg-05-11-09-02.jpg\" width=\"500\" height=\"372\"><\/p>\n<p>&nbsp;<\/p>\n<p>The Windows PowerShell version of the <b>AddHyperlinkToWord.vbs<\/b> script is called&mdash;no surprises here&mdash;<b>AddHyperlinkToWord.ps1<\/b>. This script is seen here:<\/p>\n<p><b>AddHyperlinkToWord.ps1<\/b><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">$objWord = New-Object -comobject Word.Application<br>$objWord.Visible = $true<br>$objDoc = $objWord.Documents.Add()<br>$objRange = $objDoc.Range()<br>$objLink = <br>$objDoc.HyperLinks.Add($objRange, &#8220;http:\/\/www.ScriptingGuys.com&#8221;,$null,$null,&#8221;Script Center&#8221;)<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p>The first thing that you may notice is that the two scripts, <b>AddHyperlinkToWord.ps1<\/b> and <b>AddHyperlinkToWord.vbs<\/b>, are nearly identical. Each has six lines, and each calls the same objects and methods. The one change you may notice is that the last line is shortened by using the www.scriptingguys.com URL that was not available back in 2005. Let&#8217;s examine this script line by line. <\/p>\n<p>The first step that must be performed when you work with Word Automation is to create an instance of the <b>Word.Application<\/b> COM object. To create a COM object in VBScript, you use the <b>CreateObject<\/b> method. To create a COM object in Windows PowerShell, you use the <b>New-Object<\/b><b>&ndash;comobject<\/b> cmdlet. There are two things I like about the Windows PowerShell methodology:<\/p>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"listBullet\" valign=\"top\">&bull;<\/td>\n<td class=\"listItem\">\n<p>You do not have to use parentheses and quotation marks around the program ID (<b>Word.Application<\/b>). This is because Windows PowerShell is a strongly typed language (it knows when something is a string, an integer, or a <b>datetime<\/b> value, for example) The <b>&ndash;comobject<\/b> parameter expects you to supply a string. Because it is expecting a string, you do not have to tell it you are giving it a string by using quotation objects.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" valign=\"top\">&bull;<\/td>\n<td class=\"listItem\">\n<p>You do not have to use a keyword such as <b>Set<\/b> to tell the variable that you are giving it an object. One thing that was confusing to students of VBScript was the use of the <b>Set<\/b> keyword. <b>Set<\/b> is required when you want to store an object in a variable. The problem is students were not always sure when an object was returned. In Windows PowerShell you can avoid that problem&mdash;everything is an object. Even a simple integer is an object (an instance of the <b>system.int<\/b> .NET Framework class) and has both properties and methods.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The line of the script that creates the instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb148369.aspx\" target=\"_blank\">Word.Application object<\/a> and stores the returned object in the <b>$objWord<\/b> variable is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objWord = New-Object -comobject Word.Application<\/p>\n<p><\/font><\/span><\/p>\n<p>The next line in the <b>AddHyperlinkToWord.ps1<\/b> script makes the instance of the <b>Word.Application<\/b> object visible. To do this, you use a Boolean value. In the VBScript example, you used <b>True<\/b>; here you use the automatic variable <b>$true<\/b>. I like using the automatic variables <b>$true<\/b> and <b>$false<\/b> because they work almost everywhere. In VBScript some Boolean values require you to specify <b>-1<\/b>. Others require a string &#8220;True&#8221;. Still others used the <b>TRUE<\/b> keyword:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objWord.Visible = $true<\/p>\n<p><\/font><\/span><\/p>\n<p>Now you have to add a document to hold the hyperlinks. To do this, you use the <b>Add<\/b> method from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211902.aspx\" target=\"_blank\">Documents collection<\/a>. In Windows PowerShell, all methods use empty parentheses. This provides a visual indicator you are working with a method instead of a property. In VBScript this was not a requirement, and sometimes it was confusing whether you were actually dealing with a property or a method. <\/p>\n<table id=\"EFAAC\" class=\"dataTable\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">One bug I discovered while writing the Microsoft Press book, <a href=\"http:\/\/www.microsoft.com\/mspress\/books\/authors\/auth9543.aspx\" target=\"_blank\">Microsoft VBScript Step by Step<\/a>, was on MSDN. The <b>count<\/b> method from the Windows Script Host was referred to in one place as the <b>count<\/b> property.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>In Windows PowerShell you will never confuse a method and a property. Here is how you call the <b>Add<\/b> method to add a document to the <b>documents<\/b> collection: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objDoc = $objWord.Documents.Add()<\/p>\n<p><\/font><\/span><\/p>\n<p>You now have to create a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb178841.aspx\" target=\"_blank\">range object<\/a>. To do this, you use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb241514.aspx\" target=\"_blank\">Range method<\/a> from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211897.aspx\" target=\"_blank\">Document object<\/a>. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objRange = $objDoc.Range()<\/p>\n<p><\/font><\/span><\/p>\n<p>As soon as you have the <b>Application<\/b> object, the <b>Document<\/b> object, and the <b>Range<\/b> object, you are ready to add the hyperlink. To do this, you use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb241543.aspx\" target=\"_blank\">Hyperlinks property<\/a> to create an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211980.aspx\" target=\"_blank\">Hyperlinks collection<\/a>. The <b>Hyperlinks<\/b> collection object has the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb237120.aspx\" target=\"_blank\">Add method<\/a>, which enables you to add hyperlinks to a Office Word document. These parameters are shown in <b>Table 1<\/b>.   <\/p>\n<table id=\"ENCAC\" class=\"dataTable\" cellspacing=\"0\" cellpadding=\"0\">\n<thead>\n<tr>\n<td class=\"tableHeader\" colspan=\"4\">Table 1 Parameters of the Hyperlink Add Method<\/td>\n<\/tr>\n<tr class=\"stdHeader\" valign=\"top\">\n<td id=\"colEQCAC\">Name<\/td>\n<td id=\"colEUCAC\">Required\/Optional<\/td>\n<td id=\"colEYCAC\">Data Type<\/td>\n<td id=\"colE3CAC\">Description<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">Anchor<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Required<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Object<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The text or graphic that you want turned into a hyperlink.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">Address<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The address for the specified link. The address can be an e-mail address, an Internet address, or a file name. You should be aware that Microsoft Office Word does not check the accuracy of the address.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">SubAddress<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The name of a location within the destination file, such as a bookmark, named range, or slide number.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">ScreenTip<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The text that appears as a ScreenTip when the mouse pointer is positioned over the specified hyperlink. The default value is &#8220;Address&#8221;.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">TextToDisplay<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The display text of the specified hyperlink. The value of this argument replaces the text or graphic specified by Anchor.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">Target<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The name of the frame or window in which you want to load the specified hyperlink.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>In VBScript you were allowed to pad parameters (omit parameter values) when calling methods. In Windows PowerShell you are not allowed to pad parameters. You must supply a value for each parameter, even if you only use <b>$null<\/b>. In this method call, the <b>Range<\/b> object, contained in the <b>$objRange<\/b> variable is the anchor. The string <b>http:\/\/www.<\/b><b>s<\/b><b>cripting<\/b><b>g<\/b><b>uys.com<\/b> is the address parameter. The <b>SubAddress<\/b> and <b>ScreenTip<\/b> parameters are left out. The <b>TextToDisplay<\/b> parameter value is &#8220;Script Center.&#8221; The <b>Target<\/b> parameter is left off. This is allowed because it is located on the end of the <b>method<\/b> signature. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objLink = <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objDoc.HyperLinks.Add($objRange, &#8220;http:\/\/www.ScriptingGuys.com&#8221;,$null,$null,&#8221;Script Center&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p>MF, I have to return to the Scripting Guys booth. Craig and I are in Los Angeles this week for Tech&#8729;Ed, and we are dividing our time between sessions and the booth (I am also hanging out at the Windows PowerShell team&#8217;s booth and speaking on Friday). If you are out here this week stop by the Scripting Guys booth and say hi! Craig is a really nice guy and is fun to talk to. Just don&rsquo;t expect him to fix your scripts. That&rsquo;s Ed&rsquo;s job. Ed will also talk to you about tea, Australia, scuba diving, and of course scripting. See you back here tomorrow.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I am trying to learn Windows PowerShell. I have a script that creates a Word document that has a series of hyperlinks in it. I want to be able to write that script in Windows PowerShell, but I am not sure where I should begin. As far as I can tell, there [&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":[84,49,3,4,5,155,45],"class_list":["post-53783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I am trying to learn Windows PowerShell. I have a script that creates a Word document that has a series of hyperlinks in it. I want to be able to write that script in Windows PowerShell, but I am not sure where I should begin. As far as I can tell, there [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53783","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=53783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53783\/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=53783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}