0

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
jNsatiable
  • 119
  • 3
  • 15
  • 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](https://stackoverflow.com/questions/16418292/vba-open-word-from-excel), – QHarr Oct 10 '17 at 06:55
  • @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 Oct 10 '17 at 07:37
  • I still haven't worked this thing out,. anybody, help :-| – jNsatiable Oct 27 '17 at 06:42

1 Answers1

1

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
Imran Malek
  • 1,709
  • 2
  • 13
  • 14
  • Thanks Imran for this code! I tried and believe that the only thing missing is the ability to specify the target Word document,. can that be done? As for the bookmarking stuff, the Word doc actually has dummy images in there that I assign bookmarks with. Using my code, I open the document and run the macro. It inserts the new images just before the dummy ones, then I delete the latter manually. If there's a way to delete those via code, it would be extra helpful :) – jNsatiable Oct 11 '17 at 14:14
  • Hey, Imran :) Sorry to be a pain, but is there a chance that this code can cater to "Inserting in/at bookmarks"? At the moment I've got 8 images to insert at specific locations in a 21-page file, hence the need for the bookmarks. Thank you for the objWord.Visible.Open code, though. As I am very new to coding, that should trigger some ideas for future use :) – jNsatiable Oct 12 '17 at 03:43
  • @JohnnyDerpp add one line again.Feel free to change something in the code , It won't work at worst but you'll learn a lot! – Imran Malek Oct 12 '17 at 07:07
  • @ImranMalk Thank you, I've updated the post to reflect some my issues with the code :) – jNsatiable Oct 12 '17 at 12:37
  • Hey! Was able to test your code after I had my MS OFFICE fixed by a Microsoft guy (4 hours that ultimately ended up with windows update lol). Now back to the code. It seems to insert one image to all of the bookmarks that i specify,. I tried adding "Call Fillabookmark" lines that specified other images, too, but that only resulted to all of the called images into each of the bookmark specified (i.e. images 1 to 3 in bookmark 1 , in bookmark 2 and so on). can this be solved using loop? the one specified by letter j in the original code i've posted? – jNsatiable Oct 13 '17 at 09:28
  • Oh my goodness. I just got back to this task and tried your code again,. I only had to remove the 2 extra With-EndWiths and the code worked like a charm! I don't know how it works here in Stack overflow, but I think we have to edit your solution? Anyways, thank you very much, @ImranMalek :) – jNsatiable Dec 05 '17 at 03:33