Consulting

Results 1 to 8 of 8

Thread: Renaming word bookmarks

  1. #1

    Post Renaming word bookmarks

    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.
    Last edited by Aussiebear; 04-06-2023 at 09:53 PM. Reason: Added code tags to supplied code

  2. #2
    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
    Last edited by Aussiebear; 04-06-2023 at 09:54 PM. Reason: Added code tags to supplied code

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.
    Last edited by Aussiebear; 04-06-2023 at 09:55 PM. Reason: Adjusted the code tags

  4. #4
    Banned VBAX Regular aja's Avatar
    Joined
    Aug 2007
    Posts
    15
    Location
    Murphy, try carrying the 4 and adding the endsub after the next set of traffic lights

  5. #5
    Quote Originally Posted by aja
    Murphy, try carrying the 4 and adding the endsub after the next set of traffic lights
    Errr, cheers...............................

    Bookmarks - done.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi aja,
    You just seem to be intent on wasting our time. Please regard this 7 day ban as a final warning.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ummm, yes, that seems appropriate.

  9. #9
    This thread has been solved - prior to the advice but thanks anyway!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •