Imagine you have a lot of images that you want to include in a presentation. While you are working on image two of the two hundred images, you wonder if there is an easier way?
I searched the internet for vba examples in PowerPoint. Here is one good resource I found. The solution uses a PowerPoint macro. The idee is to create a new slide for each image that is inserted into a PowerPoint presentation. The macro is started by clicking a new defined button.
Explenation of some commands and objects used in the macro:
- Dir function, Returns a String representing a list of files that matches the specified image pattern.
- A loop construction, to be able to process each and every image.
- The powerpoint slides object, used to copy the image onto a new slide.
- Image settings are copied from the slide properties, like slide height and slide width.
Quick steps:
- Create a new PowerPoint presentation
- Create a new Macro
- Copy and paste the code below in the new macro
- Save the presentation with macro
- Execute the macro
Sub insertPNGs()
Dim sourceFolder As String
Dim imageName As String
Dim currentPresentation As Presentation
Dim activeSlide As Slide
Dim activeLayout As CustomLayout
Dim x As Long
' Change the source location, mind the last "\"
sourceFolder = "C:\Temp\Images\"
Set currentPresentation = ActivePresentation
Set activeSlide = currentPresentation.Slides(currentPresentation.Slides.Count)
Set activeLayout = activeSlide.CustomLayout
imageName = Dir$(sourceFolder & "*.PNG")
While imageName <> ""
x = x + 1
With activeSlide.Shapes.AddPicture(sourceFolder & imageName, msoFalse, msoTrue, -1, -1, -1, -1)
.Cut
End With
With activeSlide.Shapes(3)
.Select
End With
ActiveWindow.View.Paste
With activeSlide.Shapes(3).Line
.Visible = True
.ForeColor.RGB = vbWhite
End With
activeSlide.Shapes.Title.TextFrame.TextRange = "Screenshot " & x
imageName = Dir()
If imageName <> "" Then
Set activeSlide = currentPresentation.Slides.AddSlide(currentPresentation.Slides.Count + 1, activeLayout)
ActiveWindow.View.GotoSlide activeSlide.SlideIndex
End If
Wend
End Sub