Skip to content

Instantly share code, notes, and snippets.

@nightscape
Created April 7, 2012 14:01
Show Gist options
  • Save nightscape/2329188 to your computer and use it in GitHub Desktop.
Save nightscape/2329188 to your computer and use it in GitHub Desktop.
Macro - Bulk import images into Powerpoint slides
' Slightly modified from http://www.pptfaq.com/FAQ00352_Batch_Insert_a_folder_full_of_pictures-_one_per_slide.htm
Sub InsertImages()
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 = "C:\Users\Someone\Pictures\"
strFileSpec = "*.jpg"
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:=100, _
Height:=100)
' Reset it to its "real" size
With oPic
.ScaleHeight 1, msoTrue
.ScaleWidth 1, msoTrue
End With
' 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
' Or (with thanks to David Marcovitz) make the picture as big as possible on the slide
' without changing the proportions
' Leave the above commented out, uncomment this instead:
With oPic
.LockAspectRatio = msoTrue
If 3 * .Width > 4 * .Height Then
.Width = ActivePresentation.PageSetup.SlideWidth
.Top = 0.5 * (ActivePresentation.PageSetup.SlideHeight - .Height)
Else
.Height = ActivePresentation.PageSetup.SlideHeight
.Left = 0.5 * (ActivePresentation.PageSetup.SlideWidth - .Width)
End If
End With
' Get the next file that meets the spec and go round again
strTemp = Dir
Loop
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment