Hey, Im creating a powershell script to search through word documents for hyperlinks. Here is what I would like to achive -
Search through the word document
Find the hyperlinks
obtain the hyperlink address
find the other word document from that hyperlink address
Then attach that document to the original document
Here is what I have so far : -
$wdStory = 6
$wdMove = 0
$application = New-Object -ComObject word.application
$root='S:\IT\Applications\Livelink\OIDocuments\Aled'
$subfolder='\Docs'
$path = "$root$subfolder"
$ifile="$($root)\Part_6_Load_runindex.txt"
$file="$($root)\debug_Part_6_Load.txt"
Get-ChildItem $path -Recurse -include *.doc, *.docx |
ForEach-Object{
$document=$application.documents.open($_.fullname)
write-host "Processing $($document.name)" -ForegroundColor green
$objSelection = $application.Selection
$a = $objSelection.EndKey($wdStory, $wdMove)
$docprops=@{
Name=$_.Name
Path=$_.DirectoryName
LinkText=$null
LinkAddress=$null
}
$document.Hyperlinks |
ForEach-Object{
$docprops.LinkText=$_.TextToDisplay
$docprops.LinkAddress=$_.Address
New-Object PSObject -Property $docprops
$fpath = $document.path;
$newaddress= $fpath.SubString(0,($fpath.LastIndexOf('\')+1))
$faddress = $_.Address
add-content $file "newaddress: $($newaddress)";
if($faddress.startswith("..\"))
{
$newaddress = $newaddress+$faddress.replace("..\","")
write-host $newaddress;
add-content $file "newaddress (..\): $($newaddress)";
write-host "another end";
}
elseif($faddress.startswith("\\"))
{
$newaddress = $faddress;
write-host $newaddress;
add-content $file "newaddress (\\): $($newaddress)";
write-host "another end";
}
else
{
#$newaddress = +$root;
#$faddress.replace("../","/")
write-host $root
$faddress = $faddress.replace("../","\")
$newaddress = $newaddress + $faddress
write-host $newaddress
}
#$objselection.InsertBreak(1);
#$insertdoc = $application.documents.open($newaddress)
}
$document.Close()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($document)
} | Format-List
[void]$application.quit()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($application)
I cannot figure out how to format the hyperlink address correctly in order for insertdoc to work correctly. Also there is something strange things going on with the loops. Overall I’m confused as I have never used powershell before. Any help out there