VBA-Excel: Add/Insert multiple Images/Pictures from a folder in Word Document
To Add or Insert Multiple Images or Pictures in Microsoft Word Document using Microsoft Excel, you need to follow the steps below:
Declare END_OF_STORY and MOVE_SELECTION as variables
Assign MOVE_SELECTION =0 and END_OF_STORY = 6
Create the object of Microsoft Word
Create the object of FileSystemObject
Create Folder object using FileSystemObject and GetFolder method
Using MS word object, Open the existing word document by providing the complete path
Make the MS Word visible
Create a Selection object with the help of WordObject.
Access Folder files using Folder object
Get the image paths from the folder
Insert image in the word documents using AddPicture()
Insert a page break after every image
Declare END_OF_STORY and MOVE_SELECTION as variables
Dim END_OF_STORY
Dim MOVE_SELECTION
Assign MOVE_SELECTION =0 and END_OF_STORY = 6
END_OF_STORY = 6
MOVE_SELECTION = 0
Create the object of Microsoft Word
Set objWord = CreateObject(“Word.Application”)
Create the object of FileSystemObject
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Create Folder object using FileSystemObject and GetFolder (link) method
Set objFolder = objFSO.GetFolder(strFolderPath)
Using MS word object, open the existing word document by providing the complete path
Set objDoc = objWord.Documents.Open(“D:\OpenMe.docx”)
Make the MS Word Visible
objWord.Visible = True
Create a Selection object with the help of WordObject.
Set objSelection = objWord.Selection
Access Folder files using Folder object
objFolder.Files
Get the image paths from the folder
ImgPath = Img.Path
Insert image in the word documents using AddPicture()
objSelection.InlineShapes.AddPicture (ImgPath)
Insert a page break after every image
objSelection.insertbreak
Complete Code:
Function FnInsertMultipleImages(strFolderPath)
Dim objWord
Dim objDoc
Dim objSelection
Dim objShapes
Dim objFSO
Dim objFolder
Set objWord = CreateObject("Word.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderPath)
Set objDoc = objWord.Documents.Open("D:\OpenMe.docx")
objWord.Visible = True
Set objSelection = objWord.Selection
For Each Img In objFolder.Files
ImgPath = Img.Path
objSelection.InlineShapes.AddPicture (ImgPath)
objSelection.insertbreak
Next
End Function
Call the function within Sub in Excel to add multi pics
Sub insertScript()
FnInsertMultipleImages(“provide the path of the folder” )
End Sub