locked
Test MS Word Document’s hyperlinks using PowerShell RRS feed

  • Question

  • I need to go through the MS Word document to find all hyperlinks and test if links are OK. Most of hyperlinks pointed to SharePoint documents.

    thank you.

    Thursday, January 31, 2013 6:25 PM

Answers

  • I am not familiar enough with Sharepoint to tell you how to access with PowerShell, but this code can be used to modify all hyperlinks in a Word document to check and replace those with a space in the link.

    Now for the gotcha...

    When viewing the hyperlinks through PowerShell, you will not be able to tell the difference between those that are using the %20 and those that still have spaces. For some reason, everything looks like it has a space in it and the script will go on and make the update to each hyperlink. You will see the change if you open up a word document and edit the hyperlink where the %20 has replaced the spaces.

    $word = New-Object -ComObject word.application
    $document = $word.documents.open("Full path to word doc")
    $hyperlinks = @($document.Hyperlinks) 
    $hyperlinks | ForEach {
        $newURI = ([uri]$_.address).AbsoluteUri
        Write-Verbose ("Updating {0} to {1}" -f $_.Address,$newURI) -Verbose
        $_.address = $newURI
    }
    $document.save()
    $word.quit()


    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat

    • Marked as answer by dod Thursday, February 21, 2013 8:57 PM
    Thursday, February 21, 2013 12:38 AM
  • You will have to look at using If/ElseIf statements to cover that piece. Depending on how many hyperlinks need replaced, you could end up with a lof of code to cover it all.

    If (_$.address -eq "http://abc.com") {
        $_.Address = "http://xyz.com"
    } ElseIf ($_.Address -eq "http://def.com") {
        $_.Address = "http://hij.com"
    }


    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat

    • Marked as answer by dod Thursday, February 21, 2013 8:57 PM
    Thursday, February 21, 2013 7:01 PM

All replies

  • What is your question?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, January 31, 2013 6:31 PM
  • Hello mjolinor,

    Sorry for delay, my task changed a little, here what I’d like to accomplish. I have hundreds of MS Word documents on SharePoint. I need PowerShell script to be able to access documents in SharePoint and perform two steps; check all Hyperlinks if there is a space in the link and replace it with %20; find certain Hyperlink and replace it with another. What do I need to access SharePoint with PowerShell, and manipulate MS Word Hyperlinks? Thank you in advance.

    Wednesday, February 20, 2013 3:00 PM
  • This doesn't solve the main piece of the question regarding reading a word document, but this bit of code will make it easier to convert the hyperlink so it replaces the spaces with "%20"

    ([uri]"http://domain.com/this is a test").AbsoluteUri
    The output will now be http://domain.com/this%20is%20a%20test

    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat


    • Edited by Boe ProxMVP Wednesday, February 20, 2013 3:33 PM
    Wednesday, February 20, 2013 3:33 PM
  • Thank you Boe Prox for the tip, replacement the spaces with "%20" works perfectly..

    Wednesday, February 20, 2013 7:09 PM
  • I am not familiar enough with Sharepoint to tell you how to access with PowerShell, but this code can be used to modify all hyperlinks in a Word document to check and replace those with a space in the link.

    Now for the gotcha...

    When viewing the hyperlinks through PowerShell, you will not be able to tell the difference between those that are using the %20 and those that still have spaces. For some reason, everything looks like it has a space in it and the script will go on and make the update to each hyperlink. You will see the change if you open up a word document and edit the hyperlink where the %20 has replaced the spaces.

    $word = New-Object -ComObject word.application
    $document = $word.documents.open("Full path to word doc")
    $hyperlinks = @($document.Hyperlinks) 
    $hyperlinks | ForEach {
        $newURI = ([uri]$_.address).AbsoluteUri
        Write-Verbose ("Updating {0} to {1}" -f $_.Address,$newURI) -Verbose
        $_.address = $newURI
    }
    $document.save()
    $word.quit()


    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat

    • Marked as answer by dod Thursday, February 21, 2013 8:57 PM
    Thursday, February 21, 2013 12:38 AM
  • Thank you so much Boe Prox, it does the job!

    Could you please also help me out with hyperlink replacement in the word document? Let’s say there is hyperlink http://abc.com/word.doc , it has to be replaced with http://xyz.com/word.doc. Document name is the same, only path is different.

    best regards,

    dod.

    Thursday, February 21, 2013 5:57 PM
  • You will have to look at using If/ElseIf statements to cover that piece. Depending on how many hyperlinks need replaced, you could end up with a lof of code to cover it all.

    If (_$.address -eq "http://abc.com") {
        $_.Address = "http://xyz.com"
    } ElseIf ($_.Address -eq "http://def.com") {
        $_.Address = "http://hij.com"
    }


    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat

    • Marked as answer by dod Thursday, February 21, 2013 8:57 PM
    Thursday, February 21, 2013 7:01 PM
  • Thank you so much for all your help.

    The part related to accessing data on the SharePoint I will open new post.

    best regards,

    dod.

    Thursday, February 21, 2013 8:56 PM