PDA

View Full Version : [SOLVED:] Renaming word bookmarks



murphy84
07-13-2007, 04:02 AM
Hi I was wondering if anyone knows how to append a string to the end of all of the bookmarks within a document and make sure the bookmark stays in the same place. I have tried the following code:-


Public Sub appendStringToBookmarks()
Dim i1 As Integer
Dim aBookMark As Bookmark
Dim aMarks() As String
Dim s1 As String
s1 = "_1"
If ActiveDocument.Bookmarks.Count >= 1 Then
ReDim aMarks(ActiveDocument.Bookmarks.Count - 1)
i1 = 0
For Each aBookMark In ActiveDocument.Bookmarks
aMarks(i1) = aBookMark.Name
aBookMark.Name = aMarks(i1) & s1
i1 = i1 + 1
Next aBookMark
End If
End Sub

Unfortunately the debugger falls down when attempting to change the aBookMark.Name property.

Can anyone advise a way around this or is creating a new bookmark and inserting it at the location of the current bookmark the only option?

Thanks in advance.

Sirfanta
07-13-2007, 05:50 AM
Try this.


Sub SettBookmarks()
Dim J As Integer
Dim BookM
BookM = "Hello"
For J = 1 To ActiveDocument.Bookmarks.Count
ActiveDocument.Bookmarks(ActiveDocument.Bookmarks(J).Name).Select
Selection.InsertAfter (BookM)
Next J
End Sub

fumei
07-13-2007, 08:05 AM
Ummm, Sirfanta, did you actually run your code?

1. It adds text after the bookmark. The new text is not in the bookmark.

2. The OP appears to be wanting to change the Name of the bookmark.

Also, why do you have BookM as a Variant variable, when you are using it as a String????

Lastly, these are equivalent:


ActiveDocument.Bookmarks(ActiveDocument.Bookmarks(J).Name).Select
ActiveDocument.Bookmarks(J).Select

You are using the index number to get the name...when the index number itself works exactly the same.


murphy84, please clarify "make sure the bookmark stays in the same place".

Is what you want a way to append text to the content of an existing bookmark, then rename it? If so:

1. do you want to make the original bookmark go away?

2. what exactly is "same place"?

Bookmark.Name is read-only-. Press F1 (Help) over .Name in your code. Notice that it is NOT including the read-write list.

In other words...you can not ONLY rename an existing bookmark.

However, if I understand correctly what you want to do, there are couple of ways to go about it.

One way. Select your bookmark text and put its string value into a variable. Delete the bookmark - not its Range, just the bookmark! Append your other string. Make the new Selection a new bookmark with whatever name you want.

aja
08-31-2007, 12:35 AM
Murphy, try carrying the 4 and adding the endsub after the next set of traffic lights

murphy84
08-31-2007, 02:23 AM
Murphy, try carrying the 4 and adding the endsub after the next set of traffic lights

Errr, cheers...............................

Bookmarks - done.

fumei
08-31-2007, 11:59 AM
If this is Solved, please mark it solved.

The bottom line is - at least as far as the Subject line is concerned - bookmarks can NOT be renamed. They can be re-created though.

mdmackillop
09-01-2007, 04:19 AM
Hi aja,
You just seem to be intent on wasting our time. Please regard this 7 day ban as a final warning.
Regards
MD

fumei
09-01-2007, 12:02 PM
Ummm, yes, that seems appropriate.

murphy84
09-03-2007, 12:15 AM
This thread has been solved - prior to the advice but thanks anyway!