PDA

View Full Version : [SLEEPER:] Add picture to bookmark in word



daniel76
04-09-2008, 07:18 PM
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

fumei
04-10-2008, 11:13 AM
You can insert a bitmap (a picture) both IN a bookmark (within the range), or AT a bookmark.


ActiveDocument.Bookmarks("here").Range _
.InlineShapes.AddPicture FileName:="c:\zzz\spoon.gif"


The above will insert the image file (spoon.gif) AT the bookmark - that is, right after the bookmark, but not INSIDE the bookmark range.



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


The above code will insert the image file INSIDE the bookmark range.

daniel76
04-10-2008, 06:24 PM
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.

fumei
04-11-2008, 09:45 AM
"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.

tiberius356
03-24-2010, 02:52 PM
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

macropod
03-24-2010, 10:45 PM
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

tiberius356
03-25-2010, 02:31 PM
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

banjobrack
11-04-2014, 02:38 PM
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?


You can insert a bitmap (a picture) both IN a bookmark (within the range), or AT a bookmark.


ActiveDocument.Bookmarks("here").Range _
.InlineShapes.AddPicture FileName:="c:\zzz\spoon.gif"


This will insrt the image file (spoon.gif) AT the bookmark - that is, right after the bookmark, but not INSIDE the bookmark range.


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
This will insert the image file INSIDE the bookmark range.

gmaxey
11-04-2014, 09:20 PM
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.

banjobrack
11-05-2014, 07:52 AM
That fixed it. Thank you so much! You saved the day... and possibly even my hair and monitor.



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.

banjobrack
03-03-2015, 11:00 AM
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