Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

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

ResolvedVersion 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:

Good coding practice

It is important that you always comment the VBA code that you write. By doing this it will make the code much easier to read, as both yourself and other people will be able to see what the code is doing or meant to be doing. This is very important if there is more than one person who writes code in the company as you may have different styles of coding and may not really understand what the other person is trying to do. Also if you write some code and then do not touch it for a year or so you may actually forget what it is meant to be doing. so it is very important to include comments throughout your code and try and make sure that they are as useful as possible

View all VBA hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.