I have been struggling with this for days and can't figure why it only inserts two photos in the first slide and quits.
I have hundreds of photos to insert stacked two to a page, the THEN & NOW.
Every slide will have a THEN photo & a NOW photo. Jpgs are named "THEN 1234, THEN 2345, THEN 3456 etc. and NOW 1234, NOW 2345, NOW 3456.
I need help figuring out what I am doing wrong that it stops after inserting photos on the first slide.
I have hundreds of photos to insert stacked two to a page, the THEN & NOW.
Every slide will have a THEN photo & a NOW photo. Jpgs are named "THEN 1234, THEN 2345, THEN 3456 etc. and NOW 1234, NOW 2345, NOW 3456.
I need help figuring out what I am doing wrong that it stops after inserting photos on the first slide.
VBA Code:
Sub InsertStackedPhotos()
Dim folderPath As String
Dim fileName As String
Dim beforePic As String
Dim afterPic As String
Dim slideIndex As Integer
Dim slide As slide
Dim shape As shape
Dim table As table
Dim picShape As shape
' Set the folder path
folderPath = "C:\Users\path\"
' Initialize slide index
slideIndex = 2
' Initialize picture paths
beforePic = ""
afterPic = ""
' Loop through the files in the folder
fileName = Dir(folderPath & "*.jpg")
Do While fileName <> ""
Debug.Print "Processing file: " & fileName ' Debug message
' Identify "Then" and "Now" photos
If Left(fileName, 6) = "THEN" Then
THENPic = folderPath & fileName
Debug.Print "Found THEN photo: " & THENPic ' Debug message
ElseIf Left(fileName, 5) = "NOW" Then
NOWPic = folderPath & fileName
Debug.Print "Found NOW photo: " & NOWPic ' Debug message
End If
' If both "THEN" and "NOW" photos are found, insert them into the current slide
If THENPic <> "" And NOWPic <> "" Then
Debug.Print "Inserting photos into slide " & slideIndex ' Debug message
' Check if the slide exists, if not, create a new one
If slideIndex > ActivePresentation.Slides.Count Then
Set slide = ActivePresentation.Slides.Add(slideIndex, ppLayoutText)
Else
Set slide = ActivePresentation.Slides(slideIndex)
End If
slideIndex = slideIndex + 1
' Add a table with 2 rows and 1 column to the slide
Set shape = slide.Shapes.AddTable(2, 1, 0, 144, 612, 576) ' 8.5 inches = 612 points, 8 inches = 576 points
Set table = shape.table
' Set the header row
table.Cell(1, 1).shape.TextFrame.TextRange.Text = "THEN"
table.Cell(2, 1).shape.TextFrame.TextRange.Text = "NOW"
' Add the "THEN" picture to the first cell
Set picShape = slide.Shapes.AddPicture(THENPic, msoFalse, msoTrue, 0, 0, -1, -1)
picShape.LockAspectRatio = msoTrue
If picShape.Width > 612 Then picShape.Width = 612 ' 8.5 inches = 612 points
If picShape.Height > 288 Then picShape.Height = 288 ' 4 inches = 288 points
' Center the picture in the cell
picShape.Top = table.Cell(1, 1).shape.Top + (table.Cell(1, 1).shape.Height - picShape.Height) / 2
picShape.Left = table.Cell(1, 1).shape.Left + (table.Cell(1, 1).shape.Width - picShape.Width) / 2
' Add the "NOW" picture to the second cell
Set picShape = slide.Shapes.AddPicture(NOWPic, msoFalse, msoTrue, 0, 0, -1, -1)
picShape.LockAspectRatio = msoTrue
If picShape.Width > 612 Then picShape.Width = 612 ' 8.5 inches = 612 points
If picShape.Height > 288 Then picShape.Height = 288 ' 4 inches = 288 points
' Center the picture in the cell
picShape.Top = table.Cell(2, 1).shape.Top + (table.Cell(2, 1).shape.Height - picShape.Height) / 2
picShape.Left = table.Cell(2, 1).shape.Left + (table.Cell(2, 1).shape.Width - picShape.Width) / 2
' Reset the picture paths for the next iteration
THENPic = ""
NOWPic = ""
End If
' Get the next file
fileName = Dir
Loop
Debug.Print "Finished processing all files." ' Debug message
End Sub