Author Posts

June 20, 2017 at 7:10 pm

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

June 20, 2017 at 8:15 pm

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()


	
			
	

June 21, 2017 at 12:03 pm

Thanks Dirk. That worked perfectly. I am curious about a couple of things here though.

1. The two lines you added at the bottom starting with [gc]. What are those doing?

2. 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

June 21, 2017 at 1:07 pm

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()