PDA

View Full Version : Adding text to a bookmark's range



MWE
08-24-2008, 02:12 PM
This is not really a VBA question, but it relates to a problem I have discovered in a VBA application.

I have recently updated to Word2003 and believe that it is behaving differently from Word2000. Assume some text has been bookmarked and Display Of Bookmarks option is turned on. So we might see:

[some bookmarked text.]

If I insert the cursor before the "end of bookmark" indicator and add something new, I would expect:

[some bookmarked text. SOME NEXT TEXT]

but what I get is:

[some bookmarked text.] SOME NEXT TEXT

Is this normal? It's a pain because if I add text to the end of a bookmarked section I have to rebookmark the resultant text (easy to do, but you have to remember to do it)

TonyJollans
08-24-2008, 03:18 PM
I can understand that it may not be what you want, but I don't think it's a change of behaviour; Word 2000 works the same way.

MWE
08-24-2008, 04:11 PM
I can understand that it may not be what you want, but I don't think it's a change of behaviour; Word 2000 works the same way.Thanks for the prompt reply

MWE
09-01-2008, 02:20 PM
I can understand that it may not be what you want, but I don't think it's a change of behaviour; Word 2000 works the same way.and it gets stranger.:banghead: I accepted the fact that the added text would be after the end of the bookmark's range and all was well (and frustrating) for a few days. Then Word2003 decided to change its behavior and reverted to the way (I believe) Word2K worked, i.e., added text is included in the bookmark's range. I went back to earlier docs and the current behavior is persistent. So now I wonder if there is some toggle inside Word that controls this phenomena ???

macropod
09-02-2008, 10:55 PM
Hi MWE,

The correct way to update the bookmarked text in vba is to use code like:
Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
If Documents.Count > 0 then
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
ActiveDocument.Bookmarks.Add BmkNm, BmkRng
End if
End if
Set BmkRng = Nothing
End subwhere BmkNm is the bookmark name and NewTxt is the new bookmark text.

lucas
09-03-2008, 06:52 AM
macropod,
so this would be used as a function basically and you would call it from another sub, something like:


Sub test()
Dim bm1 As Bookmark
UpdateBookmark "bm1", "Your Text"
End Sub

where bm1 is the name of the book mark you wish to change and Your text would be the new text in the bookmark.

I edited this to add the quotation marks for the strings....

MWE
09-03-2008, 01:11 PM
Hi MWE,

The correct way to update the bookmarked text in vba is to use code like:
Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
If Documents.Count > 0 then
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
ActiveDocument.Bookmarks.Add BmkNm, BmkRng
End if
End if
Set BmkRng = Nothing
End subwhere BmkNm is the bookmark name and NewTxt is the new bookmark text.Thanks. Actually, my post was not about updating bookmarks via VBA, but just doing it manually. That said, I have had some trouble changing bookmark text with VBA and this approach may help

macropod
09-03-2008, 01:31 PM
Hi lucas,

Yes, that's the approach you'd take, though even for the calling routine, the paramteres you're passing could also be variables - and they don't need to have the same names as those used in the UpdateBookmark sub. They could, for example, be useform variables or Excel cell contents.