Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing text in word bookmark

Status
Not open for further replies.

WindmillChaser

Technical User
Dec 27, 2005
3
US
I have an application where I fill in the values of the bookmarks in a word template. I need to loop a file to add up the quantity. At the first pass, I assigned the value to the bookmark thus,

objDoc.Bookmarks("Quantity").Range.Text = Qty1

When it loops around it is assigned the new value

objDoc.Bookmarks("Quantity").Range.Text = Qty2

However, when this is printed on the document I see, Qty1Qty2. I had expected Qty2 to overwrite Qty1.

When I look at the value of the bookmark after the first loop, it shows nothing. This again is unexpected for me.

What is the proper way to do this?

Thanks.
 
You may try something like this:
With objDoc.Application.Selection
.GoTo what:=-1, Name:="Quantity" '-1=wdGoToBookmark
.Collapse Direction:=0 '0=wdCollapseEnd
objDoc.Bookmarks("Quantity").Range.Text = Qty1
.MoveEnd unit:=1, Count:=Len(Qty1) '1=wdCharacter
objDoc.Bookmarks.Add Name:="Quantity", Range:=objDoc.Application.Selection.Range
.Collapse Direction:=0 '0=wdCollapseEnd
End With

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The point of PH's code is that Bookmark("Whatever").Range.Text = "blah blah" does NOT make that text "blah blah" the .Range.Text of the bookmark.

You would think it does...but it does not.
Code:
ActiveDocument.Bookmarks("Test").Range.Text = "blah blah"
MsgBox ActiveDocument.Bookmarks("Test").Range.Text
will display a blank message. Run that again, you will have:

blah blahblah blah

and .Range.Text will STILL be "", nothing. If you ever want to truly put text into a bookmark (rather than USE a bookmark), then code like PH's is needed. This is absolutely required if the bookmark text value needs to be REused, which is what you are doing. Here is the Sub I generically use for filling bookmarks. It is the same as PH's, but you pass the bookmark name, and the text you want to put in it, as string parameters.
Code:
Sub FillABookmark(strBM_Name As String, strBM_Text As String)
Dim oBM As Bookmarks
Set oBM = ActiveDocument.Bookmarks
  With Selection
    .GoTo what:=wdGoToBookmark, Name:=strBM_Name
    .Collapse Direction:=wdCollapseEnd
       oBM(strBM_Name).Range.Text = strBM_Text
    .MoveEnd unit:=wdCharacter, Count:=Len(strBM_Text)
       oBM.Add _
          Name:=strBM_Name, Range:=Selection.Range
    .Collapse Direction:=wdCollapseEnd
   End With
Set oBM = Nothing
End Sub
Of course if you are using an object instance of the document (like PH) then you have to fully qualify that.

Gerry
My paintings and sculpture
 
Thank you all. With your help, I now understand I made the mistake of equating,

objDoc.Bookmarks("Quantity").Range.Text = Qty1

as an assigment, when in fact the statement means type "Qty1" at the bookmark location. In order to overtype it, I must actually select "Qty1" by collapsing the pointer and then extending it over "Qty1". The code now works perfectly.

Thanks again.:-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top