Be the first user to complete this post

  • 0
Add to List

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
Word-Insert-Image-Multiple
Word-Insert-Image-Multiple



Also Read:

  1. VBA-Excel: Writing Text to Word document
  2. VBA-Excel: Open word document using GetObject()
  3. VBA-Excel: Add Worksheets For All The Given Dates Except Weekends and Copy The Common Template In Each Worksheet
  4. VBA-Excel: Appending Text to an Existing Word Document - at the End
  5. VBA-Excel: Get ALL The Opened Internet Explorer (IE) using Microsoft Excel
  6. Introduction to Excel WorkBook
  7. VBA-Excel: Open an Existing Word Document
  8. VBA-Excel: Open and Print the Word Document