This post may contain affiliate links. Please read my disclosure for more info.
Power Query Course

Edit links in PowerPoint using VBA

Edit links in PowerPoint using VBA

In a previous post, we considered how to link Excel files to PowerPoint presentations.  This can be a significant time saver as the PowerPoint presentation can be updated automatically.  Once you have created the links, life is brilliant, until . . . you need to change which file the PowerPoint presentation is linked to.  You could use the “edit links” option.  But there is one big issue with PowerPoint, you have to edit the links one by one.  If you have 100 links to different Excel workbooks you have to update each of them.  If you have 100 links to the same Excel workbook you would expect to be able to update them all at the same time.  But that’s just not how it works unfortunately, you still have to update each link individually.

Have no fear – VBA to the rescue.  Below you will find VBA code for PowerPoint and for Excel.  Yes, that’s right, you can update the links to a PowerPoint document from within Excel.  Pretty cool eh?

Here are a few notes for using the code:

  • The Excel code has been written using Early Binding.  Don’t worry if you don’t know what that means.  In the Excel Visual Basic Editor select Tools -> References -> Microsoft PowerPoint x.xx Object Library.  This will ensure that can Excel and speak PowerPoint.
  • When changing file paths remember to end the file path with a slash “\”

Edit the linked documents using PowerPoint VBA

Open the Visual Basic Editor in PowerPoint (ALT+F11).

Right-click on the VBAProject, select Insert -> Module.

PowerPoint VBA insert Module

Copy the code below into the code window.

PowerPoint VBA copy code

Sub EditPowerPointLinks()

Dim oldFilePath As String
Dim newFilePath As String
Dim pptPresentation As Presentation
Dim pptSlide As Slide
Dim pptShape As Shape

'The old file path as a string (the text to be replaced)
oldFilePath = "String of\File Path\To Be Replaced\Excel File.xlsx"

'The new file path as a string (the text to replace with)
newFilePath = "String of\New File Path\Excel File 2.xlsx"

'Set the variable to the PowerPoint Presentation
Set pptPresentation = ActivePresentation

'Loop through each slide in the presentation
For Each pptSlide In pptPresentation.Slides

    'Loop through each shape in each slide
    For Each pptShape In pptSlide.Shapes
   
        'Find out if the shape is a linked object or a linked picture
        If pptShape.Type = msoLinkedPicture Or pptShape.Type _ 
        = msoLinkedOLEObject Then

            'Use Replace to change the oldFilePath to the newFilePath
            pptShape.LinkFormat.SourceFullName = Replace(LCase _
            (pptShape.LinkFormat.SourceFullName), LCase(oldFilePath), newFilePath)
            
        End If
    Next
Next

'Update the links
pptPresentation.UpdateLinks


End Sub

You just need to set the values for the newFilePath and oldFilePath variables listed in the code above.

Edit the linked documents from Excel using VBA

It is also possible to update the links in PowerPoint from within Excel.

Press ALT + F11 to open the Excel Visual Basic Editor.  Click Insert -> Module. Copy the code below into the new Module.

The code below uses Early Binding, so you will need to set the references to the PowerPoint object library.  Select Tools -> References, then select the Microsoft PowerPoint x.xx Object Library from the list.

Sub EditPowerPointLinks()

'Set the link to the Object Library: 
'Tools -> References -> Microsoft PowerPoint x.xx Object Library

Dim oldFilePath As String
Dim newFilePath As String
Dim sourceFileName As String
Dim pptApp As PowerPoint.Application
Dim pptPresentation As Object
Dim pptSlide As Object
Dim pptShape As Object

'The file name and path of the file to update
sourceFileName = "C:\File Path\Of Source File\File Name.pptx"

'The old file path as a string (the text to be replaced)
oldFilePath = "String of\File Path\To Be Replaced\Excel File.xlsx"

'The new file path as a string (the text to replace with)
newFilePath = "String of\New File Path\Excel File2.xlsx"

'Set the variable to the PowerPoint Application
Set pptApp = New PowerPoint.Application

'Make the PowerPoint application visible
pptApp.Visible = True

'Set the variable to the PowerPoint Presentation
Set pptPresentation = pptApp.Presentations.Open(sourceFileName)

'Loop through each slide in the presentation
For Each pptSlide In pptPresentation.Slides

    'Loop through each shape in each slide
    For Each pptShape In pptSlide.Shapes
   
        'Find out if the shape is a linked object or a linked picture
        If pptShape.Type = msoLinkedPicture Or pptShape.Type _
        = msoLinkedOLEObject Then

            'Use Replace to change the oldFilePath to the newFilePath
            pptShape.LinkFormat.SourceFullName = Replace(LCase _
            (pptShape.LinkFormat.SourceFullName), LCase(oldFilePath), newFilePath)

        End If
    Next
Next

pptPresentation.UpdateLinks

'Save, close and quit the application
pptPresentation.Save
pptPresentation.Close
pptApp.Quit


'Release the memory
Set pptApp = Nothing
Set pptPresentation = Nothing
Set pptSlide = Nothing
Set pptShape = Nothing

End Sub

For the code above it will be necessary to set the values for the sourceFileName, oldFilePath and newFilePath variables.

Save

13 thoughts on “Edit links in PowerPoint using VBA

  1. Stacey says:

    I am trying to get this to work using Office 16. I am not sure what I am missing but I have tried both the excel method and the ppt method.
    Any suggestions?

    I have 12 spreadsheets with about 30 links each to the same spreadsheet.

    Thank you so much for an insight you can provide.

    ‘The file name and path of the file to update
    sourceFileName = “\\mikewin10\C$\Users\mdavis\Desktop\Budgets\Sales Meeting 2019\A-Jan 2019.pptm\”

    ‘The old file path as a string (the text to be replaced)
    oldFilePath = “\\mikewin10\C$\Users\mdavis\Desktop\Budgets\Sales Meeting 2018\2018 Source.xlsx\”

    ‘The new file path as a string (the text to replace with)
    newFilePath = “\\mikewin10\C$\Users\mdavis\Desktop\Budgets\Sales Meeting 2019\2019 Source.xlsx\”

    • Excel Off The Grid says:

      Hi Stacey,

      Thanks for your question.

      My guess is that you need to remove the \ from the end of the file path. Let me know if that works.

  2. Gadi Bizinyan says:

    Hi Mark, just wanted to tell you that the info you wrote here is priceless! Last week I had to deal with a project wherein a pair of files inter-connected (Excel feeding PowerPoint through linked ranges across 50+ slides) kept changing locations together as a pair. The problem how to keep this pair connected at all times despite path changes as well as the Excel file renaming. The solution was to update all the links of the PowerPoint presentation from within Excel and the technique in my macro was based on the code you introduced. Saved me a lot of time writing it myself from scratch. Brilliant piece of work. Thank you for sharing.

    One comment – The variable pptApp is also an abject and at the end of the code you ought to set it to Nothing together with all the other object variables.

  3. Allen says:

    Hi Mark, I can’t get either the powerpoint macro or the excel macro to run correctly. In both cases I can see the macro running, but no changes take place to the ppt. For example when I run the macro in Excel, I see it open the Powerpoint file, it spins for a while, then closes the Powerpoint. But when I re-open the Powerpoint it’s the exact same as it was. Any thoughts? Thanks!

    • Excel Off The Grid says:

      Hi Allen – I’m guessing that the issues are likely to be either:

      (1) the text string set for the oldFilePath is not the same as the text string in the PowerPoint file.

      (2) the after making the change, the newFilePath is not a valid file. PowerPoint will only update if the path is valid and exists.

      • Allen says:

        Hi Mark, I eventually figured it out. My PPT contained charts, which the code for PPT doesn’t account for. So I just added “Or pptShape.Type = msoLinkedChart” before the ‘Then’ in the section of the code that finds out if the shape is a linked object or a linked picture.

        Thanks for your help and quick reply, very kind of you and very much appreciated.

  4. Aurélie says:

    Hi, Thank you for this ! I am totally new to VBA so perhaps a stupid question but does it also work if a Powerpoint presentation is linked to 3 different excel files ? How should i adapt the code ? thx !! u

    • Excel Off The Grid says:

      The code works based on a Find & Replace of the file path and file name. So it depends specifically on the find text string you’re using as to whether it will match multiple linked files.

Leave a Reply

Your email address will not be published. Required fields are marked *