I am able to add multiple images to a word document using VBA, but I am not able to add captions and its hyperlinks for multiple images loaded from the 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
Following code provides this:
- insert a text "Table of Figures:" at the beginning of the document
- add a table of figures
- add each picture pof your directory (incl. its name as caption below and a page break)
- update the table of figures
If you add a reference to
Microsoft Word x.x Object Library
you can use early binding. That means you could use the self-explanatory ENUM values I noted as comments.The pictures are stored as links in the document, as the document my get very large if you store them completely (refer to AddPicture).