How do I use Powershell to print a list of hyperlinks that appear in a Word document?

Posted by dwwilson66 on Stack Overflow See other posts from Stack Overflow or by dwwilson66
Published on 2013-10-21T19:25:05Z Indexed on 2013/10/22 21:54 UTC
Read the original article Hit count: 720

Filed under:
|

I asked this question last week, and my script cannot find any hyperlinks to change. Now I'm backing up and trying to simply open a single document and list the hyperlinks within. I've verified that the document includes hyperlinks to a number of anchors in the same document, and two hyperlinks to documents in the same directory. However, either Powershell isn't finding links in the doc or I'm outputting the list to the console improperly.

Here's my bare-bones code

$word = New-Object -ComObject Word.Application
$doc ="R:\path\Reporting_Emergency_Or_Hazardous_Situation_-_BC_CC.doc" 
$hyperlinks = @($doc.Hyperlinks)
$hyperlinks
$word.quit()

or, for line 4

Write-Host $hyperlinks

or, again for line 4

$hyperlinks | % {Write-Host $_.address}

No errors, just blank results. Not even an object reference to the $hyperlinks array.

When I modify line 4 to

% $address in $hyperlinks {
    Write-Host $._address
    }

I get the following error...but it's unclear if I'm trying to read a null array? or if the value is blank.

ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "in" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
At F:\path\HyperLinkScrub.ps1:46 char:2
+ % <<<<  $address in $hyperlinks {
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

What am I missing here? I've verified that the Word doc has hyperlinks, and ultimately, I'm trying to diagnose if my script isn't looking for them properly, or if I'm not outputting them to the console properly.

© Stack Overflow or respective owner

Related posts about powershell

Related posts about hyperlink