Hello all.
I have a user who has created Powerpoint documents that includes the full url in the hyperlinks. Unfortunately, that sharepoint server no longer exists, and every link needs to be changed. I have a script that can find all the hyperlinks, and I can reformat them to the new server. However, when I SAVE the presentation, it is not saving the change. Any thoughts?
$oldserver = "henniges.hennigesauto.com"
$newserver = "hennisphere.hennigesautomotive.com"
Add-Type -AssemblyName Office
$objPPT = New-Object -ComObject powerpoint.Application
$Doc = $objPPT.Presentations.Open("D:\Caeg.pptx")
$Slides = $Doc.Slides
Foreach ($Slide in $Slides) {
$Links = $Slide.Hyperlinks | Select Address
Foreach ($Link in $Links) {
Write-Host "[BEFORE]" $Link.Address
If ($Link.Address -like "*$oldserver*") {
$NewAddress = "https://" + $newserver + $Link.Address.substring($oldserver.Length + 7)
$Link.Address = $NewAddress
Write-Host "[AFTER]" $Link.Address
}
}
}
$Doc.Save()
$Doc.Close()
Any thoughts why it is not saving the links in the doc?
Thanks,
Des
dirk
June 20, 2017, 2:45pm
2
The part where you use “| select Address” makes the code fail you could use something like this instead:
Add-Type -AssemblyName Office
$objPPT = New-Object -ComObject powerpoint.Application
$Doc = $objPPT.Presentations.Open("D:\Caeg.pptx")
$Slides = $Doc.Slides
Foreach ($Slide in $Slides) {
$Links = $Slide.Hyperlinks
Foreach ($Link in $Links) {
If ($Link.Address -like "*$oldserver*") {
$NewAddress = "https://" + $newserver + $Link.Address.substring($oldserver.Length + 7)
$Link.Address = $NewAddress
Write-Host "[AFTER]" $Link.Address
}
}
}
$Doc.Save()
$Doc.Close()
$objPPT = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
</pre<
Thanks Dirk. That worked perfectly. I am curious about a couple of things here though.
The two lines you added at the bottom starting with [gc]. What are those doing?
Is there a way to close the Powerpoint window? The presentation gets closed, but the window remains open. I can’t find a method to close it.
Thansks again,
Des
dirk
June 21, 2017, 7:37am
4
The purpose of 1) was intended to achieve 2) :-). But I actually missed to call quit() of the application object. I’d also recommend to hide the ppt window while the automation runs. This is the modified version:
Add-Type -AssemblyName Office
$objPPT = New-Object -ComObject powerpoint.Application
#open but hide
$Doc = $objPPT.Presentations.Open("D:\Caeg.pptx", $Null,$Null,[Microsoft.Office.Core.MsoTriState]::msoFalse)
$Slides = $Doc.Slides
Foreach ($Slide in $Slides) {
$Links = $Slide.Hyperlinks
Foreach ($Link in $Links) {
If ($Link.Address -like "*$oldserver*") {
$NewAddress = "https://" + $newserver + $Link.Address.substring($oldserver.Length + 7)
$Link.Address = $NewAddress
}
}
}
$Doc.Save()
$Doc.Close()
$objPPT.Quit()
$objPPT = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()