Sub MailMergeWithTwoImages()
Dim ppt As Presentation
Dim slide As slide
Dim excelApp As Object
Dim wb As Object
Dim ws As Object
Dim i As Integer
Dim nameText As String
Dim imagePath1 As String
Dim imagePath2 As String
Dim shape As shape
Dim imgShape1 As shape
Dim imgShape2 As shape
Dim originalSlide As slide
Dim newSlide As slide
Dim imagePlaceholder1 As shape
Dim imagePlaceholder2 As shape
' Initialize Excel Application and Workbook
Set excelApp = CreateObject("Excel.Application")
Set wb = excelApp.Workbooks.Open("D:\AUTOMATION\ExcelFile.xlsx")
Set ws = wb.Sheets(1)
' Get the active PowerPoint presentation
Set ppt = ActivePresentation
' Ensure there's at least one slide in the presentation
If ppt.Slides.Count > 0 Then
Set originalSlide = ppt.Slides(1)
i = 2
Do While ws.Cells(i, 1).Value <> ""
' Get the name and image paths from the Excel sheet
nameText = ws.Cells(i, 1).Value
imagePath1 = ws.Cells(i, 2).Value
imagePath2 = ws.Cells(i, 3).Value ' Assuming column C holds the second image path
' Add a new slide and copy shapes from the original slide
Set newSlide = ppt.Slides.Add(ppt.Slides.Count + 1, ppLayoutText)
' Copy all shapes from original slide
For Each shape In originalSlide.Shapes
shape.Copy
newSlide.Shapes.Paste
Next shape
' Replace text placeholder (assuming title placeholder contains "[Title]")
For Each shape In newSlide.Shapes
If shape.HasTextFrame Then
If shape.TextFrame.TextRange.Text Like "*[Title]*" Then
shape.TextFrame.TextRange.Text = Replace(shape.TextFrame.TextRange.Text, "[Title]", nameText)
End If
End If
Next shape
' Find placeholders for two images
For Each shape In newSlide.Shapes
If shape.Name = "MyImage1" Then
Set imagePlaceholder1 = shape
ElseIf shape.Name = "MyImage2" Then
Set imagePlaceholder2 = shape
End If
Next shape
' Insert first image
If Dir(imagePath1) <> "" Then
If Not imagePlaceholder1 Is Nothing Then
Set imgShape1 = newSlide.Shapes.AddPicture(imagePath1, _
MsoTriState.msoFalse, MsoTriState.msoCTrue, _
imagePlaceholder1.Left, imagePlaceholder1.Top, _
imagePlaceholder1.Width, imagePlaceholder1.Height)
Else
MsgBox "First image placeholder not found.", vbExclamation, "Error"
End If
Else
MsgBox "First image not found: " & imagePath1, vbExclamation, "Error"
End If
' Insert second image
If Dir(imagePath2) <> "" Then
If Not imagePlaceholder2 Is Nothing Then
Set imgShape2 = newSlide.Shapes.AddPicture(imagePath2, _
MsoTriState.msoFalse, MsoTriState.msoCTrue, _
imagePlaceholder2.Left, imagePlaceholder2.Top, _
imagePlaceholder2.Width, imagePlaceholder2.Height)
Else
MsgBox "Second image placeholder not found.", vbExclamation, "Error"
End If
Else
MsgBox "Second image not found: " & imagePath2, vbExclamation, "Error"
End If
i = i + 1
Loop
Else
MsgBox "No slide to copy from. Please ensure the presentation has at least one slide.", vbCritical, "Error"
End If
' Clean up
wb.Close False
excelApp.Quit
Set excelApp = Nothing
End Sub
Category: Automation
-
How to Automatically Insert Two Images into Microsoft PowerPoint Slides
-
Mail Merge in Powerpoint – Single Image Each Slide automatically
Sub MailMergeWithImages() Dim ppt As Presentation Dim slide As slide Dim excelApp As Object Dim wb As Object Dim ws As Object Dim i As Integer Dim nameText As String Dim imagePath As String Dim shape As shape Dim imgShape As shape Dim originalSlide As slide Dim newSlide As slide Dim imagePlaceholder As shape Set excelApp = CreateObject("Excel.Application") Set wb = excelApp.Workbooks.Open("D:\AUTOMATION\ExcelFile.xlsx") Set ws = wb.Sheets(1) Set ppt = ActivePresentation If ppt.Slides.Count > 0 Then Set originalSlide = ppt.Slides(1) i = 2 Do While ws.Cells(i, 1).Value <> "" nameText = ws.Cells(i, 1).Value imagePath = ws.Cells(i, 2).Value Set newSlide = ppt.Slides.Add(ppt.Slides.Count + 1, ppLayoutText) For Each shape In originalSlide.Shapes shape.Copy newSlide.Shapes.Paste Next shape For Each shape In newSlide.Shapes If shape.HasTextFrame Then If shape.TextFrame.TextRange.Text Like "*[Title]*" Then shape.TextFrame.TextRange.Text = Replace(shape.TextFrame.TextRange.Text, "[Title]", nameText) End If End If Next shape For Each shape In newSlide.Shapes If shape.Name = "MyImages" Then Set imagePlaceholder = shape Exit For End If Next shape If Dir(imagePath) <> "" Then If Not imagePlaceholder Is Nothing Then Set imgShape = newSlide.Shapes.AddPicture(imagePath, _ MsoTriState.msoFalse, MsoTriState.msoCTrue, _ imagePlaceholder.Left, imagePlaceholder.Top, _ imagePlaceholder.Width, imagePlaceholder.Height) Else MsgBox "Image placeholder not found.", vbExclamation, "Error" End If Else MsgBox "Image not found: " & imagePath, vbExclamation, "Error" End If i = i + 1 Loop Else MsgBox "No slide to copy from. Please ensure the presentation has at least one slide.", vbCritical, "Error" End If wb.Close False excelApp.Quit Set excelApp = Nothing End Sub