I would want images to be inserted into bookmarks in MS Word document (.docx) with the use of Excel. I stumbled upon a Word VBA workaround that is almost perfect for the job EXCEPT that it is of course, a code that sits in Word (I just save it in the global template). The reason why I would need it to be in Excel is because I can't save the macro in the .docx file --- I can't afford to save it as a macro-enabled document as it will mess up with the existing VBA in Excel (Another person made it :). I did have exhausted all effort Googling but there is no exact solution for this. For reference, here is the 'modified' code that I was talking about. I copied it from user fumei in vbaexpress.com
Sub FillABookmark(strBM As String, strText As String)
Dim j As Long
With ActiveDocument
.Bookmarks(strBM).Range _
.InlineShapes _
.AddPicture FileName:=strText
j = ActiveDocument.InlineShapes.Count
.InlineShapes(j).Select
.Bookmarks.Add strBM, Range:=Selection.Range
End With
End Sub
Sub InsertScreenshots()
Call FillABookmark("Image_1", "C:\Users\Public\Documents\Image1.png")
Call FillABookmark("Image_2", "C:\Users\Public\Documents\Image_2.png")
Call FillABookmark("Image_3", "C:\Users\Public\Documents\Image_3.png")
End Sub
I would appreciate any kind of help :)
Update:
Shoutout to Imran :) Your code has been a great help, but I can't seem to work it off to work for multiple images,.. I can't even all of the things that my attempts did, but all of them sort of pastes the new images to one and the same bookmark. Plus a failing Office 365 to add to the dilemma,. I'm reinstalling it later and will on be available for comment tomorrow :( I'm out of my wits and tried to incorporate the looping feature in the original code that I posted. The following code is my failed attempt at it:
Sub FillABookmark(bookmarkname As String, imagepath As String)
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "D:\test.docx"
Set objDoc = objWord.activedocument
With objDoc
.Bookmarks("test1").Select
.Shapes.AddPicture Filename:=imagepath
End With
With objDoc
.Bookmarks("test2").Select
.Shapes.AddPicture Filename:=imagepath
End With
With objDoc
.Bookmarks("test3").Select
.Shapes.AddPicture Filename:=imagepath
End With
End Sub
Sub InsertScreenshots()
Call FillABookmark("test1", "C:\Users\Public\Documents\image_1.png")
Call FillABookmark("test2", "C:\Users\Public\Documents\image_2.png")
Call FillABookmark("test3", "C:\Users\Public\Documents\iamge_3.png")
End Sub