PDA

View Full Version : Renameing Word Bookmark



MWE
09-01-2008, 01:57 PM
I am running Word2003. I have a document with several hundred bookmarks each of a particular format. I want to "rename" each bookmark to a slightly different format. Looping through the bookmarks and changing the name of each would be easy if the Name property were not Read-Only. So, I sequence through bookmarks, identify those that are of the particular format that I wish to change, create the new name, set a range object = to the current bookmark's range, delete the current bookmark, create a new bookmark with the new name and the "saved" range. Seems to work except the formatting of the text is no longer what it was. Whatever was the format for the leading character of the original bookmark's text is now applied to every character of the new bookmark's text.

Any thoughts re why the text associated with the new bookmark is different? I could keep a copy of the original text and then format the new text as per the original, but that seems like a waste of time.

Thanks

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

What's your code? It's a bit hard to suggest what changes you should make without seeing it.

MWE
09-03-2008, 10:37 PM
Hi MWE,

What's your code? It's a bit hard to suggest what changes you should make without seeing it.I was able to find a way to fix the problem, but I am not sure why it worked. In addition to renaming the bookmark, I also needed to change a few characters in the bookmark. So, I used:

bkmk.Range.Text = Replace(bkmk.Range.Text,OldText,NewText)

The length of OldText and NewText is the same and the char position is 15 characters or so into the bkmk's range. This approach worked but when I removed that code, the problems with the formatting disappeared; so that approach must have been the reason for screwing up the format (?).

When I changed the approach to setting a separate Range to the portion of text I wanted to change and then setting the other range's text to the next text:

I1 = bkmk.Range.Start + Offset
I2 = I1 + Len(OldText) - 1
set rngOther = ActiveDocument.Range(I1, I2)
rngOther.Text = NewText

The formatting problems disappeared.:dunno

macropod
09-05-2008, 05:06 AM
Hi MWE,

I've already posted some code to help you modify a bookmark's contents. Here's some code to 'rename' all the bookmarks in a document:
Sub RenameBkMrks()
Dim oBkMrk As Bookmark
Dim BmkRng As Range
Dim i As Integer
i = 0
If ActiveDocument.Bookmarks.Count > 0 Then
For Each oBkMrk In ActiveDocument.Bookmarks
Set BmkRng = ActiveDocument.Bookmarks(oBkMrk).Range
i = i + 1
ActiveDocument.Bookmarks.Add "BkMrk" & i, BmkRng
oBkMrk.Delete
Next oBkMrk
End If
End SubBe warned: If you've got any cross-references to the existing bookmarks, they'll also need to be modified to point to the 'renamed' versions. It would be best to do that at the same time as you doo the renaming. Also, if you're working with formfield-based bookmarks, you need to modify the bookmark names assigned by the formfields.

MWE
09-05-2008, 10:48 AM
Hi MWE,

I've already posted some code to help you modify a bookmark's contents. Here's some code to 'rename' all the bookmarks in a document:
Sub RenameBkMrks()
Dim oBkMrk As Bookmark
Dim BmkRng As Range
Dim i As Integer
i = 0
If ActiveDocument.Bookmarks.Count > 0 Then
For Each oBkMrk In ActiveDocument.Bookmarks
Set BmkRng = ActiveDocument.Bookmarks(oBkMrk).Range
i = i + 1
ActiveDocument.Bookmarks.Add "BkMrk" & i, BmkRng
oBkMrk.Delete
Next oBkMrk
End If
End SubBe warned: If you've got any cross-references to the existing bookmarks, they'll also need to be modified to point to the 'renamed' versions. It would be best to do that at the same time as you doo the renaming. Also, if you're working with formfield-based bookmarks, you need to modify the bookmark names assigned by the formfields.Thanks for the reply and suggestions. My initial approach was somewhat similar to your code above and did not work. I tried your code on my system (Word2003, WinXP) and it fails with the same problem I encountered initially. The oBkMk.Delete statement causes a loop failure and the procedure aborts without any error messages. I suspect that the failure is because the loop variable "no longer exists"(?); but I do not know why no error message is generated -- another Word VBA mystery. I substitutedFor I = 1 to ActiveDocument.Bookmarks.Count
set oBkMk = ActiveDocument.BookMarks(I)
.
.
.
oBkMk.Delete
Next Iand it works fine. I had sorted that out before I posted about the formatting problem.