creating macro replaces image

Forum home » Delegate support and help forum » Microsoft VBA Training and help » Creating a macro that replaces an image in multiple PowerPoints

Creating a macro that replaces an image in multiple PowerPoints

resolvedResolved · High Priority · Version 2010

Catherine has attended:
Excel Intermediate course
Excel Advanced course
Excel VBA Intro Intermediate course

Creating a macro that replaces an image in multiple PowerPoints

Hi there, Please can you help me with the following VBA. I am trying to replace the first image that is on slide 1 of multiple Powerpoints within a folder. I want to replace the image with a new image that is saved on my desktop. I am also deleting another image on the page. The names vary from powerpoint to powerpoint in a series of 500 which is why I am looping this through a folder of powerpoints. I am not worried about the positioning of the logo yet I can do that bit I just can't get this code to open a series without crashing. Any help would be appreciated.

Private Sub CommandButton1_Click()
Dim oPPApp As Object, oPPPrsn As Object
Dim oPPSlide As Object, oPPShape1 As Object, oPPShape2 As Object
Dim FlName As String
Dim gfilename As String
Dim strfilename As String
Dim strfoldername As String
Dim pp As Presentation
strfoldername = "C:\PowerPoint folder"
strfilename = Dir(strfoldername & "\*.ppt*")
Do While Len(strfilename) > 0
'~~> Change this to the relevant file
FlName = strfilename
gfilename = "C:\Users\me\Desktop\logo.jpg"
'~~> Establish an PowerPoint application object
On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")
If Err.Number <> 0 Then
Set oPPApp = CreateObject("PowerPoint.Application")
End If
Err.Clear
On Error GoTo 0
oPPApp.Visible = True
'~~> Open the relevant powerpoint file
Set oPPPrsn = oPPApp.Presentations.Open(FlName)
'~~> Change this to the relevant slide which has the shape
Set oPPSlide = oPPPrsn.Slides(1)
'~~> This is the shape which will be replaced
Set oPPShape1 = oPPSlide.Shapes(1)
oPPShape1.Delete
Set oPPShape2 = oPPSlide.Shapes(2)
oPPShape2.Delete
pp.Save
pp.Close
Loop
End Sub

There is one line of code which seems to be failing and not opening up the Powerpoint. Are you able to see the error?

Cheers, Catherine

RE: Creating a macro that replaces an image in multiple PowerPoi

Hi Catherine,

Thank you for the forum question.

I have tested your code and unfortunately I have not been able to find a solution. I can see one thing which is wrong but it will not fix the problem not opening the presentation but it will open the next file in the source folder.

When you are using the Dir function you have build in a loop in the function. To open the next file in the folder you need to tell the function when. If you add the line as I have done below it will open the next presentation.

pp.Save
pp.Close
strfilename = Dir() "THIS LINE"
Loop
End Sub

Unfortunately this will not fix your problem, but I hope that you will find a solution.



Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Fri 19 Sep 2014: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

VBA tip:

Count the Rows and Columns in a Selection

If you need to count the number of rows or columns in a worksheet use the following code:

Selection.Rows.Count - Returns the number of rows in the selection

Selection.Columns.Count - Returns the number of columns in the selection

Selection.CurrentRegion.Rows.Count - Returns the number of rows in the current region of the selection

View all VBA hints and tips


Server loaded in 0.11 secs.