VBA-Excel: Add/Insert multiple Images/Pictures from a folder in Word Document

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值