Consulting

Results 1 to 11 of 11

Thread: add picture to bookmark in word

  1. #1

    add picture to bookmark in word

    Hi,

    I'm trying to insert a picture into a location where i have chosen in the word report document throught a userform but to no avail.

    Not sure if i can add picture into bookmark. I have a userform that will require user to insert the full path of the picture file. When user hits OK button, i would like the picture to be added into the bookmark that has been set up before. Does it work - the bookmark and the picture? or the bookmark accepts text only.

    If bookmark doesn't work with picture, what's other alternative way of inserting picture into the spot we like it to be in the word document?

    Thank you in advance

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You can insert a bitmap (a picture) both IN a bookmark (within the range), or AT a bookmark.
    [vba]
    ActiveDocument.Bookmarks("here").Range _
    .InlineShapes.AddPicture FileName:="c:\zzz\spoon.gif"
    [/vba]

    This will insrt the image file (spoon.gif) AT the bookmark - that is, right after the bookmark, but not INSIDE the bookmark range.
    [vba]
    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 TryMe()
    Call FillABookmark("here", "c:\zzz\spoon.gif")
    End Sub
    [/vba]This will insert the image file INSIDE the bookmark range.

  3. #3
    Thank you Gerry,

    It sort of works. It puts in the picture within the bookmarks. However because i have a text in the bookmark, that text also appears there. The text doesn't get overwritten or replaced by the picture. Instead, we have a picture and the text next to the picture. But i guess i can redo the bookmarks by not inserting a text in the bookmarks. So, i think i know how to solve it from here.

    Thank you once again for your help.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "It sort of works. It puts in the picture within the bookmarks. However because i have a text in the bookmark, that text also appears there. "

    No, it does work, exactly as you asked for - "i would like the picture to be added into the bookmark that has been set up before".

    As you never mentioned that there was existing text...how was I to know? It always helps if you actually mention things like that in the first place.

    The code has to be adjusted to do something with any existing text.

    "by not inserting a text in the bookmarks"

    So you have other stuff going on that puts text into the bookmarks?

    In any case, you seem to be well enough along to fix things.

  5. #5
    Fumie,

    I have read your posts, excellent help you have given to many, bravo.

    I need to replace an existing image in a bookmark, with a new gif stored on a drive...

    Can you show how to remove/delete etc the image inside the bookmark and replace with a new/substitute image/gif...

    thank you in advance, or anyone else who knows how to do this...

    greatly appreciated

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Tiberius,

    Try something based on:
    Sub UpdateBookmarkedImage(BmkNm As String, NewTxt As String)
    Dim BmkRng As Range
    With ActiveDocument
      If .Bookmarks.Exists(BmkNm) Then
        Set BmkRng = .Bookmarks(BmkNm).Range
        BmkRng.InlineShapes(1).Delete
        BmkRng.InlineShapes.AddPicture FileName:=NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      End If
    End With
    Set BmkRng = Nothing
    End Sub
    which you can call with code like:
    Sub UpdateImage()
    Dim BmkNm, NewTxt As String
    BmkNm = "MyBookmarkName"
    NewTxt = "C:\Users\Tiberius\Pictures\MyPic.jpg"
    UpdateBookmarkedImage BmkNm, NewTxt
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    Macropod,

    Firstly, thank you. Secondly you are another great contributor and someone who when i read their posts i am grateful you write them...so on behalf of those others, thank you.

    I implemented something similar, using a very similar approach but i have some further complications to incorporate into my solution here at work..will post back after the weekend - once i get to apply what you have written.

    Also i will post about templates (.dot) being added to a normal (.doc) and saving it - with embedded objects being "unlinked" in the saveas operation. Please consider looking at that question

    Tiberius

  8. #8

    Useful code, but new problem.

    I've also found this code very helpful and have been using it in several of my documents at work. These last two weeks, I've stumbled upon a new problem. I have a bookmark titled "Logo" at the top of the page that inserts the appropriate logo depending upon which selection is made from the ComboBox dropdown.

    This has worked great, but recently I came across a document and wanted to use this code, but there is a second .jpg image I'll refer to as "signature" at the bottom of the document. This "signature" image is not tied to a bookmark. When I click the command button to run the code, the Logo bookmark populates with the appropriate logo, but then the bookmark marker at the logo section disappears and a bookmark is placed around the "signature" image at the bottom.

    Since it changes the bookmark location, I can't clear everything and repopulate without closing out the document altogether each time, which makes this confusing for my end-users.

    I would love some assistance if you can help. Please keep in mind, I'm new to all of this, so I if you can put it in 5th grader-speak, that would be awesome.

    Please help?

    Quote Originally Posted by fumei View Post
    You can insert a bitmap (a picture) both IN a bookmark (within the range), or AT a bookmark.
    [vba]
    ActiveDocument.Bookmarks("here").Range _
    .InlineShapes.AddPicture FileName:="c:\zzz\spoon.gif"
    [/vba]

    This will insrt the image file (spoon.gif) AT the bookmark - that is, right after the bookmark, but not INSIDE the bookmark range.
    [vba]
    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 TryMe()
    Call FillABookmark("here", "c:\zzz\spoon.gif")
    End Sub
    [/vba]This will insert the image file INSIDE the bookmark range.

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Your problem is likely related to:

    j = ActiveDocument.InlineShapes.Count
    .InlineShapes(j).Select

    Change .InlineShapes(j).Select to .InlineShapes(1).Select and see if that helps.
    Greg

    Visit my website: http://gregmaxey.com

  10. #10

    Red face Did you ever know that you're my hero -- Bette Midler

    That fixed it. Thank you so much! You saved the day... and possibly even my hair and monitor.


    Quote Originally Posted by gmaxey View Post
    Your problem is likely related to:

    j = ActiveDocument.InlineShapes.Count
    .InlineShapes(j).Select

    Change .InlineShapes(j).Select to .InlineShapes(1).Select and see if that helps.

  11. #11
    So, now I have a similar project, but having issues with it as well. If I have two bookmarks (Logo and Logo2 on separate pages), is there a way to get both bookmarks to populate with the corresponding image (based on ComboBox selection) without losing the bookmark field(s)? 5th-grader speak is appreciated.

    Using the following:
    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(1).Select
    .Bookmarks.Add strBM, Range:=Selection.Range
    End With
    End Sub

    Sub CheckList ()
    If ComboBox1.Value = "Company A" Then
    Call UpdateBookmark("Logo", "c:\zzz\ALogo.jpg")
    Call FillABookmark("Logo", "c:\zzz\ALogo.jpg")
    Call UpdateBookmark("Logo2", "c:\zzz\ALogo.jpg")
    Call FillABookmark("Logo2", "c:\zzz\ALogo.jpg")

    End If

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •