The Situation: I am running a macro to import graphics from a folder to PowerPoint, 4 per slide in a custom format, using a macro I found here.
The Issue: The order the pictures are imported is not done by the name of the file. How could I import these photos in that order? My files are named as Chart 1, Chart 2, etc.
The Code:
Sub InsertQuadFormat()
Dim presentation
Dim layout
Dim slide
Dim FSO
Dim folder
Dim file
Dim folderName
Dim i As Integer
'Change the folder as per your needs
folderName = "C\"
i = 1
Set presentation = Application.ActivePresentation
If presentation.Slides.Count > 0 Then
presentation.Slides.Range.Delete
End If
Set layout = Application.ActivePresentation.SlideMaster.CustomLayouts(3)
Set FSO = CreateObject("Scripting.FileSystemObject")
Set folder = FSO.GetFolder(folderName)
' loop though each image in the folder
For Each file In folder.Files
If LCase(Mid(file.Name, Len(file.Name) - 3, 4)) = ".png" Then
If i Mod 4 = 1 Then
' For 1,5,9 .... images
Set slide = presentation.Slides.AddSlide(presentation.Slides.Count + 1, layout)
While slide.Shapes.Count > 0
slide.Shapes(1).Delete
Wend
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 15
.Top = 70
.Height = 460
.Width = 460
End With
ElseIf i Mod 4 = 2 Then
' For 2,6,10 .... images
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 484
.Top = 70
.Height = 460
.Width = 460
End With
ElseIf i Mod 4 = 3 Then
' For 3,7,11 .... images
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 15
.Top = 296
.Height = 460
.Width = 460
End With
Else
' For 4,8,12 .... images
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 484
.Top = 296
.Height = 460
.Width = 460
End With
End If
End If
i = i + 1
Next
End Sub