Bulk Changing Hyperlinks in PowerPoint Presentations

Welcome Forums General PowerShell Q&A Bulk Changing Hyperlinks in PowerPoint Presentations

Viewing 3 reply threads
  • Author
    Posts
    • #73327
      Participant
      Topics: 5
      Replies: 7
      Points: 15
      Rank: Member

      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
      Participant
      Topics: 0
      Replies: 19
      Points: 0
      Rank: Member

      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<
    • #73358
      Participant
      Topics: 5
      Replies: 7
      Points: 15
      Rank: Member

      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
      Participant
      Topics: 0
      Replies: 19
      Points: 0
      Rank: Member

      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()
      
      
      
Viewing 3 reply threads
  • The topic ‘Bulk Changing Hyperlinks in PowerPoint Presentations’ is closed to new replies.