Insert pictures in a folder into a Word document (VBA)

Introduction

This VBA sample shows how to insert many pictures in a folder into a Word document by one picture per page.

Scenarios

Customers have multiple pictures in a folder and they need to automatically insert one picture per page in Word as it will take a lot of time if operating from UI. So they need a script to accomplish that.

Script

Step1. Open the Word document that you want to insert pictures into.

Step2. Press Alt+F11 to open the VBE in Word.

Step3. Focus on the specified document project, select the file (insertimage.bas) and drag it to the Project or via File >> Import File... (Ctrl+M).
 

Step4. Go back to the document.

Step5. Press the Run button via Alt+F8.

Step6. Select "InsertImage" in the names list, and then click the Run button. We will see a window which is used to select a folder.
 

Note 1. Please make sure that macros are enabled in Word.

For Word 2010:
File >> Options >> Trust Center >> Trust Center Settings >> Macro Settings >> Enable all macros >> OK >> OK >> Restart Word

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

VB Script
Edit|Remove
Sub InsertImage() 
    Dim FolderPath, objFSO, Folder, ImagePath, image 
    Const END_OF_STORY = 6Const MOVE_SELECTION = 0 
    FolderPath = Select_Folder_From_Prompt 
    If InStr(FolderPath, "EMPTY") = 0ThenSet objFSO = CreateObject("Scripting.Filesystemobject") 
        Set Folder = objFSO.GetFolder(FolderPath) 
        ForEach image In Folder.Files 
            ImagePath = image.Path 
            If CheckiImageExtension(ImagePath) = TrueThen'Insert the images into the word document 
                Application.Selection.EndKey END_OF_STORY, MOVE_SELECTION 
                Application.Selection.InlineShapes.AddPicture (ImagePath) 
                Application.Selection.InsertBreak  'Insert a pagebreakEndIfNextEndIfEndSub

 

Prerequisite

Office 2010 or Higher version

Additional Resources

Related forum threads:
How to insert multiple pictures with type per page
Adding image per each page in word document