0
votes

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
1
Does the existing Excel VBA expect a specific kind of Word file extension e.g. .docx, you could change this to expect .docm or .docb Otherwise for starters, in Excel, you will need to add a reference to the Word object library (if using early binding) or code that includes CreateObject("Word.Application") if late binding Open word from Excel,QHarr
@Johnny Derpp your question is unclear , could you redefined your question ? perhaps a more shorter version would help us understand what you want.Looking at the detailed question I could deduce that you only want to insert pictures in word document via excel.It doesn't say anything about the bookmark.Imran Malek
I still haven't worked this thing out,. anybody, help :-|Johnny Derpp

1 Answers

1
votes

If it's only the image that you want to add in a word document then use this,

  Sub FillABookmark(bookmarkname As String, imagepath As String)
    Dim objWord As Object
    Dim objDoc As Object

    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If objWord Is Nothing Then
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objWord.Documents.Open "D:\imran.docx"
    End If
    Set objDoc = objWord.activedocument

    With objDoc
        .Bookmarks(bookmarkname).Select
        .Shapes.AddPicture Filename:=imagepath
    End With
    With objDoc
        .Bookmarks(bookmarkname).Select
        .Shapes.AddPicture Filename:=imagepath
    End With
    With objDoc
        .Bookmarks(bookmarkname).Select
        .Shapes.AddPicture Filename:=imagepath
    End With

End Sub

Sub InsertScreenshots()
    Call FillABookmark("test", "C:\Users\Public\Documents\1.jpg")
End Sub