Powershell - Deleting Hyperlinks from a dox/docx file.

Hi, I would like to create a script that goes into doc/docx files and deletes the hyperlink and the hyperlink formatting. This is what I have so far -

add-type -AssemblyName "Microsoft.Office.Interop.Word" #Adding the assembly for working with word documents to allow us to change paper size and orientation later

Get-ChildItem $path -Recurse -include *.doc, *.docx | #For loop to retreve information about the documents such as their name, file path and the number of hyperlinks inside it
    ForEach-Object{
        $document=$application.documents.open($_.fullname) #opening the documents
        write-host "Processing $($document.name)" -ForegroundColor green #letting the user know what document the script is working on currently
        $objSelection = $application.Selection #select the document
        $a = $objSelection.EndKey($wdStory, $wdMove)
        $docprops=@{
            Name=$_.Name
            Path=$_.DirectoryName
           LinkText=$null
            LinkAddress=$null
            }
            $document.Hyperlinks | #for each loop working on the hyperlinks
            ForEach-Object{
                $docprops.LinkText=$_.TextToDisplay
                $docprops.LinkAddress=$_.Address
                New-Object PSObject -Property $docprops #Printing out the document properties such as hyperlink text and its address
                $fpath = $document.path;
                $newaddress= $fpath.SubString(0,($fpath.LastIndexOf('\')+1))
                $faddress = $_.Address
                }
                $document.save()
                $document.Close() #close the document
                [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($document) #Release the write lock on the Word document
         }
         $application.quit()
         [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($application) #Release the write lock on the application

This code searches for doc/docx files in the folders provided in $path/$subfolder, then opens them and searches them for hyperlinks. If they are found, print them out to screen. What I would now like to do is remove the hyperlinks found and the hyperlink formatting if that's possible?

Any Help out there?

Thanks.

July 30th, 2014 10:57am

Hi Rundownbassman,

To delete the Hyperlinks from a dox/docx file, the script below for your reference, which can delete all hyperlinks and remain the text in docx file, you can make a test file firstly:

$word = New-Object -ComObject word.application
$document = $word.documents.open("d:\test.docx")
$hyperlinks = @($document.Hyperlinks) 
$hyperlinks | ForEach {
 $_.Delete()
}
$document.save()
$document.Close()
$word.quit()

Best Regards,

Anna Wang

Free Windows Admin Tool Kit Click here and download it now
August 1st, 2014 4:48am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics