Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2024, 09:12 AM
ctviggen ctviggen is offline Changing name of both a bookmark and its cross reference Windows 10 Changing name of both a bookmark and its cross reference Office 2016
Advanced Beginner
Changing name of both a bookmark and its cross reference
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default Changing name of both a bookmark and its cross reference

I insert a cross-reference to a numbered item in Word, and I insert the reference to the paragraph number of a numbered paragraph/item. I want to change both the cross-reference AND the "hidden" bookmark to which the cross reference points. Here's my research so far.



The cross-reference to the numbered item looks like "_RefSome#", e.g., _Ref157502046. To see these, alt-F9.

These are cross-references to a "hidden" bookmark associated with the numbered paragraph. To see these, Ctrl-shift-F5, ensure the box "Hidden bookmarks" is checked. The hidden bookmarks are the same format: _RefSome#.

So, the idea should be that you find a bookmark and cross-reference with the same name (_RefSome#) and change that. (Often, there is one bookmark and multiple cross-references, but the idea is the same.) Simple, huh?


(Note: Another idea is to change all the "_Ref" to something else, like "New_", for both bookmarks and cross-references, which is what the code below does for the bookmarks only. This is probably the best idea for what I want to do.)

However, if you open the window by Ctrl-shift-F5, to see hidden bookmarks, and click the "Go To" button, the ENTIRE paragraph is somehow the bookmark.

Doing a simple change of the bookmark name, as below, causes the entire paragraph to be renamed (I copy text selected from an "openDoc" and paste it into a new document, newDoc, which is where "newDoc" comes from):

Code:
    
    For Each bookmark In newDoc.Bookmarks
        ' Check if the bookmark name starts with "_Ref" (assuming this pattern)
        If Left(bookmark.Name, 4) = "_Ref" Then
            bookmark.Range.Text = "New_" & Mid(bookmark.Name, 5)
        End If
     Next bookmark

That is, any numbered paragraph with a hidden bookmark becomes New_Some#, where "Some#" is the Some# from _RefSome# of the bookmark/cross-reference. All of the original text of the paragraph is gone, leaving only New_Some#, although the paragraph is still numbered, eg, "1. New_Some#".

But you can use the hidden bookmark window to delete a hidden bookmark, and nothing happens to the paragraph to which the bookmark is attached.

I'm totally flummoxed. How can I change the bookmark without changing the entire paragraph?
Reply With Quote
  #2  
Old 02-24-2024, 10:50 AM
Italophile Italophile is online now Changing name of both a bookmark and its cross reference Windows 11 Changing name of both a bookmark and its cross reference Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

It is not possible to change the name of a bookmark, as the documentation indicates.

Instead, you must add a new bookmark then delete the original. Done in this order you can set the Range of the new bookmark to the Range of the original.

Your code changes the text marked by the bookmark to the bookmark name.
Reply With Quote
  #3  
Old 02-24-2024, 11:29 AM
ctviggen ctviggen is offline Changing name of both a bookmark and its cross reference Windows 10 Changing name of both a bookmark and its cross reference Office 2016
Advanced Beginner
Changing name of both a bookmark and its cross reference
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

I figured it out. This is a test subroutine I wrote with some help from ChatGPT. I had to modify its code, though, and the majority of the code is what I already use. The second subroutine and a few other statements (in a different order) are ChatGPT's though.


The input is a document that has a section copied, where that section has numbered paragraphs with hidden bookmarks and cross-references to the numbered paragraphs. After the section is copied, you put the cursor where you want the new code to be pasted. There is a lot of other code that's missing where I manipulate the text. I also left in what didn't work, so you can avoid doing those things.


Code:
Sub TestBookmarks1()
    Dim newDoc As Document
    Dim openDoc As Document
    Dim aRng As Range
    Dim newName As String
    ' Dim oldName As String
    Dim bookmarkRange As Range

    Set openDoc = ActiveDocument
    
    Set aRng = Selection.Range
    
    Set newDoc = Documents.Add
    newDoc.Content.Paste

    For Each bookmark In newDoc.Bookmarks
        ' Check if the bookmark name starts with "_Ref" (assuming this pattern)
        If Left(bookmark.Name, 4) = "_Ref" Then
            ' Generate new name
            newName = "New_" & Mid(bookmark.Name, 5)
            ' Rename the bookmark - this causes an error, as Name is read-only
            ' bookmark.Name = newName
            ' Save bookmark range
            Set bookmarkRange = bookmark.Range
            ' An attempt to use bookmark.Delete where it is, but this did not work - not sure why
            ' Set oldName = bookmark.Name
            ' Delete the old bookmark - if this is here, the update does not work, as bookmark.Name is deleted
            ' bookmark.Delete
            ' Add a new bookmark with the updated name
            newDoc.Bookmarks.Add newName, bookmarkRange
            ' Update the cross-references to point to the new bookmark name
            ' UpdateCrossReferences openDoc, bookmark.Name, newName
            UpdateCrossReferences newDoc, bookmark.Name, newName
            ' UpdateCrossReferences newDoc, oldName, newName
            ' Delete the old bookmark with the _Ref name
            bookmark.Delete
        End If
    Next bookmark

    ' Copy the modified content back to the original document
    aRng.FormattedText = newDoc.Range.FormattedText
    ' Close the temporary document without saving changes
    newDoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Sub UpdateCrossReferences(doc As Document, oldName As String, newName As String)
    Dim fld As field
    Dim rng As Range

    For Each fld In doc.Fields
        If fld.Type = wdFieldRef Then
            Set rng = fld.Code
            Do While InStr(rng.Text, oldName) > 0
                rng.Text = Replace(rng.Text, oldName, newName)
            Loop
        End If
    Next fld
End Sub
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing name of both a bookmark and its cross reference Macro to delete cross-reference error message when bookmark deleted grahamsims Word 8 12-03-2020 10:50 AM
Changing name of both a bookmark and its cross reference Find {text} and insert cross reference from bookmark Slamzor Word VBA 1 12-01-2017 05:12 PM
Changing name of both a bookmark and its cross reference Cross-reference does not maintain formatting from bookmark frannie Word 2 03-23-2017 04:33 PM
Changing name of both a bookmark and its cross reference Weird issue with bookmark and cross-reference Maddog32 Word 2 06-10-2016 02:39 PM
Changing name of both a bookmark and its cross reference Need help with using bookmark and cross-reference mpdsal Word 1 07-26-2012 01:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:03 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft