Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

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

 

 

You may also like...

8 Responses

  1. Zloty potok says:

    You need to be a part of a contest for one of the most useful websites online.
    I’m going to highly recommend this site!

  2. Michael Grandal says:

    Hi. Can the code be modified to insert multiple objects (Create from file, display as icons) into an word document? Can this be done without involving Excel?

    thanks
    Michael

  3. Shyam says:

    My excel sheet has 10 rows, and i have a folder called “Input” which has 10 files of different/unknown file types. my job is to insert those 10 files in 10 appropriate rows as objects. Usually i insert one file by another one by going Insert->Object->Create from file.
    Its time consuming and ideally tough especially when i am having 100+ files to insert as specified above. It would be great if anyone posts the answer. Kindly help and save me.

  4. prakaskh says:

    Little confused. I would like to know the codes like, “sub insertpic()……….end sub”.. This article started only with “Function…..End Fuction”.. I am not aware of the remaining things. Could you please provide the entire program.. Sorry. I am new into this field.

    • Sumit Jain says:

      you can just call the function inside your sub()

      Example:

      Sub insertScript()
      FnInsertMultipleImages(“provide the path of the folder” )
      End Sub

      Is this what your were looking for???

      Thanks
      Sumit

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: