Log in

View Full Version : Copy slide, change picture



NaturalTvven
08-08-2014, 01:43 PM
Greetings,

I am attempting my first marco in powerpoint. I am simply trying to copy a 'reference' slide and replace one picture. I will do this for all pictures in a folder. The script I have adds all the slides, but then adds all the pictures to the final slide. I don't understand why this is the case since the Duplicate Slide code and the Add Picture code are in the same while loop. Can someone tell me why?


Sub LoopThroughFiles()
Dim StrFile As String
Dim Folder As String
Dim sld As Slide
Dim referencesld As Slide
Set referencesld = ActivePresentation.Slides(1)
Dim i As Integer
Dim shp As Shape
i = 1
Folder = "c:\E1B8\ScriptTesting\MISC\PPT\SampleData2\"
StrFile = Dir(Folder & "*")
Set referencesld = ActivePresentation.Slides(1)
Set shp = referencesld.Shapes(5)
Do While Len(StrFile) > 0
Debug.Print Folder & StrFile
referencesld.Duplicate
i = i + 1
Set sld = ActivePresentation.Slides(i)
With shp
sld.Shapes.AddPicture(Folder & StrFile, msoFalse, msoTrue, .Left, .Top, .Width, .Height).IncrementRotation 360#
End With
sld.Shapes(5).Delete
StrFile = Dir
Loop
End Sub



Thanks!!

John Wilson
08-11-2014, 03:19 AM
Every time you increment i Slides(i) is pointing at the last slide {The duplicate is Slides(2)}

Try this


Sub LoopThroughFiles()
Dim StrFile As String
Dim Folder As String
Dim sld As Slide
Dim Dupesld As SlideRange
Dim referencesld As Slide
Set referencesld = ActivePresentation.Slides(1)
Dim shp As Shape
Folder = "c:\E1B8\ScriptTesting\MISC\PPT\SampleData2\"
StrFile = Dir(Folder & "*")
Set referencesld = ActivePresentation.Slides(1)
Set shp = referencesld.Shapes(5)
Do While Len(StrFile) > 0
Set Dupesld = referencesld.Duplicate
With shp
Dupesld(1).Shapes.AddPicture Folder & StrFile, msoFalse, msoTrue, .Left, .Top, .Width, .Height
End With
Dupesld(1).Shapes(5).Delete
StrFile = Dir
Loop
End Sub