Add All Images From A Folder to A PowerPoint Presentation Using VBA
In this article I will explain how you can add all the images from a target folder to a power point presentation using VBA.
Contents
Step 1: Get all Files in a directory
The first step is to write a code that will retrieve all files in a directory. This can be done using the function below:
Private Function GetAllFilesInDirectory(ByVal strDirectory As String) As Variant Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim arrOutput() As Variant Dim i As Integer 'Create an instance of the FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") 'Get the folder object Set objFolder = objFSO.GetFolder(strDirectory) ReDim arrOutput(0) i = 1 'loops through each file in the directory and prints their names and path For Each objFile In objFolder.Files 'print file path arrOutput(i - 1) = objFile.Path ReDim Preserve arrOutput(UBound(arrOutput) + 1) i = i + 1 Next objFile ReDim Preserve arrOutput(UBound(arrOutput) - 1) GetAllFilesInDirectory = arrOutput End Function
The function receives as input a directory path and returns an array of all files in the directory:
Dim arrFilesInFolder As Variant arrFilesInFolder = GetAllFilesInDirectory("C:\Users\Public\Pictures\Sample Pictures")
Step 2: Creating a new Slide and Adding an Image:
The function below receives as input the path of an image file and:
1- Creates a new slide.
2- Inserts the image.
Private Function AddSlideAndImage(ByVal strFile As String) Dim objPresentaion As Presentation Dim objSlide As Slide Set objPresentaion = ActivePresentation Set objSlide = objPresentaion.Slides.Add(1, PpSlideLayout.ppLayoutChart) Call objSlide.Shapes.AddPicture(strFile, msoCTrue, msoCTrue, 100, 100) End Function
Calling Context:
Call AddSlideAndImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg")
Putting it All Together:
Sub main() Dim i As Integer Dim arrFilesInFolder As Variant arrFilesInFolder = GetAllFilesInDirectory("C:\Users\Public\Pictures\Sample Pictures") For i = LBound(arrFilesInFolder) To UBound(arrFilesInFolder) Call AddSlideAndImage(arrFilesInFolder(i)) Next End Sub Private Function GetAllFilesInDirectory(ByVal strDirectory As String) As Variant Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim arrOutput() As Variant Dim i As Integer 'Create an instance of the FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") 'Get the folder object Set objFolder = objFSO.GetFolder(strDirectory) ReDim arrOutput(0) i = 1 'loops through each file in the directory and prints their names and path For Each objFile In objFolder.Files 'print file path arrOutput(i - 1) = objFile.Path ReDim Preserve arrOutput(UBound(arrOutput) + 1) i = i + 1 Next objFile ReDim Preserve arrOutput(UBound(arrOutput) - 1) GetAllFilesInDirectory = arrOutput End Function Private Function AddSlideAndImage(ByVal strFile As String) Dim objPresentaion As Presentation Dim objSlide As Slide Set objPresentaion = ActivePresentation Set objSlide = objPresentaion.Slides.Add(1, PpSlideLayout.ppLayoutChart) Call objSlide.Shapes.AddPicture(strFile, msoCTrue, msoCTrue, 100, 100) End Function
2 thoughts on “Add All Images From A Folder to A PowerPoint Presentation Using VBA”