Hi ! I am new at VBA, and I have been trying to find a code to insert a batch of pictures into Powerpoint (2010) slides sorted by name. I found this code on http://www.pptfaq.com/FAQ00352_Batch..._per_slide.htm which works fine but it seems to be inserting the pictures a random order, I want the pictures to be sorted alphabetically by name like I see them when I go into the folder. Here is the code:
Private Sub CommandButton24_Click()
Dim strTemp As String
Dim strPath As String
Dim strFileSpec As String
Dim oSld As Slide
Dim oPic As Shape
' Edit these to suit:
strPath = "H:\Pictures\"
strFileSpec = "*.*"
strTemp = Dir$(strPath & strFileSpec)
Do While strTemp <> ""
Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
Set oPic = oSld.Shapes.AddPicture(FileName:=strPath & strTemp, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=-1, _
Height:=-1)
' width/height of -1 tells PPT to import the image at its "natural" size
' Optionally, make it fill the slide - even if that means changing the proportions of the picture
' To do that, uncomment the following:
With oPic
.LockAspectRatio = msoFalse
.Height = ActivePresentation.PageSetup.SlideHeight
.Width = ActivePresentation.PageSetup.SlideWidth
End With
' Optionally, add the full path of the picture to the image as a tag:
With oPic
.Tags.Add "OriginalPath", strPath & strTemp
End With
' Get the next file that meets the spec and go round again
strTemp = Dir$
Loop
End Sub
The file names I want in order are in this format: Image3, Image 4, Image 9, etc.
Thank you so much!
Bookmarks