0
votes

I am able to add multiple images to a word document using VBA, but I am not able to add captions for multiple images loaded from folder path.
Can you please suggest on this:

Sub checking()
    Dim strFolderPath
    strFolderPath = "C:\images"
    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:\myfile.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 Sub
1
Please edit your question and describe, how the captions should be, e. g. file name of each picture as text below it or headline above it?Asger
Captions should be inserted in the below of the each imaged loaded in to the word document in VBA. The code shows the loading of all images but not adding the file name of each below it.srihari
Where is your code embedded, in Excel?Asger

1 Answers

0
votes

By this you get following order:
- the picture with a paragraph sign
- the image path with a paragraph sign
- a page break

For Each img In objFolder.Files
    imgpath = img.Path
    objSelection.InlineShapes.AddPicture (imgpath)
    objSelection.InsertParagraphAfter
    objSelection.InsertAfter imgpath
    objSelection.InsertParagraphAfter
    objSelection.Collapse wdCollapseEnd
    objSelection.InsertBreak
Next

Additionally I suggest to use Option Explicit at the beginning of each module, which forces a declaration of every variable (e. g. Dim ImgPath as String).