Bulk Changing Hyperlinks in PowerPoint Presentations

Tagged: 

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Dirk Dirk 3 days, 9 hours ago.

  • Author
    Posts
  • #73327
    Profile photo of Des Davies
    Des Davies
    Participant

    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

  • #73330
    Profile photo of Dirk
    Dirk
    Participant

    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()
    
    
    		
    	
  • #73358
    Profile photo of Des Davies
    Des Davies
    Participant

    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

  • #73364
    Profile photo of Dirk
    Dirk
    Participant

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

You must be logged in to reply to this topic.