Adding HyperLinks to Word via Powershell

by jf781 at 2013-02-19 09:13:57

I am using a login script to create e-mail signatures in Outlook. I have everything setup except for some hyperlinks don’t appear to format correctly. Each on is adding “#0” to the end of the link.

Here is part of the script I am having trouble with.

$WordApp = New-Object -ComObject Word.Application
$Document = $WordApp.Documents.Add()
$Selection = $WordApp.Selection

$Selection.Hyperlinks.Add($Selection.Range,“mailto:”+$Email,$false,“mailto:$Email”, $Email)
$selection.TypeText(" | “)
$Selection.Hyperlinks.Add($Selection.Range,“https://”+$CompanyWebsite,$false,$CompanyWebsite,$CompanyWebsite)
$Selection.TypeParagraph()


Another part within the script saves the document to a html file that and here is that section from within the file. You can see after each href tag it adds a “#0”.


<p class=MsoNormal style=‘margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal’><span style=‘font-family:“Calibit”,“serif”’><a
href="mailto:user@compnay.com#0title="mailto:user@company.com"><span
style=‘font-family:“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;
mso-hansi-theme-font:minor-latin’>user@company.com</span></a> | <a
href=“https://www.company.com/#0title=www.company.com><span
style=‘font-family:“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;
mso-hansi-theme-font:minor-latin’>www.company.com</span></a><o:p></o:p></span></p>


I was playing around with “HyperLinks” Property and was trying to find some more information on it (No luck) and noticed with the following command I don’t get the “#0” after the hyperlinks but also I lose my Screen Pop and it shows the full URL instead of an abbreviated one.

$WordApp = New-Object -ComObject Word.Application
$Document = $WordApp.Documents.Add()
$Selection = $WordApp.Selection

$Selection.Hyperlinks.Add($Selection.Range,“mailto:”+$Email)
$selection.TypeText(" | “)
$Selection.Hyperlinks.Add($Selection.Range,“https://”+$CompanyWebsite)
$Selection.TypeParagraph()


I’m at a lose at this point. Any advice would be greatly appreciated or if someone could point me to where I could find more information on the “Hyperlinks” property I would send them bacon.
by jf781 at 2013-02-21 05:26:01
Figured it out. Instead of using $false I should have used $null. I found this Page that talked about VBScript and it broke down the items for the Hyperlinks property. Thought it might apply to Powershell (and got lucky). I was setting the sub address to nothing instead of just having word omit that part of the hyperlink entirely.

$Selection.Hyperlinks.Add($Selection.Range,“mailto:”+$Email,$null,“mailto:$Email”, $Email)
$selection.TypeText(” | ")
$Selection.Hyperlinks.Add($Selection.Range,“https://”+$CompanyWebsite,$null,$CompanyWebsite,$CompanyWebsite)
$Selection.TypeParagraph()


Dumb mistake - Hope this helps someone else.