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
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!
Thx for the motivation 🙂
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
its a vb script, so you can use it without excel as well.
put the code in an text file, save it as “.vbs” (e.g test.vbs ), you can hard code the file path or get it at the run time from the user, in that you have to modify the code a bit (http://excel-macro.tutorialhorizon.com/vba-excel-addinsert-multiple-objects-from-a-folder-in-an-excel-document/).
and that it, double click the file and its done 🙂
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.
I wrote the article for that, Thanks for a suggestions for article 🙂
Please refer
http://excel-macro.tutorialhorizon.com/vba-excel-addinsert-multiple-objects-from-a-folder-in-an-excel-document/
Let me know if this what you were looking for
Thanks
Sumit
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.
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