Manipulating cross-referencing in Word VBA macro for numbered paragraphs using text from an original document

46 Views Asked by At

I have asked this to Chat GPT. Here is part of my statement:

I have a macro. The idea behind the macro is that there is an open document (openDoc), where a user has selected formatted paragraphs and copied them into the clipboard. The copied paragraphs have the following structure of first, second, and third paragraphs:

  1. Text
  2. The text of 1, ....
  3. The text of 1 or 2, ...

These are numbered paragraphs using styles. Additionally, the "1" in the second paragraph (numbered 2) is a cross reference to the first paragraph using a numbered item with an inserted reference to a paragraph number. Similarly, the "1" in the third paragraph is the same, and the "2" in the third paragraph is a cross reference to the second paragraph using a numbered item with an inserted reference to a paragraph number.

The macro opens a new document (newDoc). The copied text from openDoc is pasted into newDoc and is manipulated. The styles and cross-referencing is maintained. The manipulated text from newDoc is pasted back into the original document (openDoc) at a location where the user has put the cursor. This works well.

In newDoc, the original cross-references are there, so that when the manipulated text is pasted into the original document, the original cross-referencing is retained. The styles are also retained. The text in the original document looks like the following after the manipulated text from newDoc is added to openDoc:

  1. Text
  2. The text of 1, ....
  3. The text of 1 or 2, ...
  4. Manipulated Text
  5. Manipulated text of 1, ....
  6. Manipulated text of 1 or 2, ...

I would rather update the cross-referencing so that the end result of the macro is the following:

  1. Text
  2. The text of 1, ....
  3. The text of 1 or 2, ...
  4. Manipulated Text
  5. Manipulated text of 4, ....
  6. Manipulated text of 4 or 5, ...

That is, the cross-referencing for the manipulated text follows the same structure and the original paragraphs but is updated to the newly added paragraphs. This means paragraph 5 has a cross-reference to paragraph 4; paragraph 6 has cross-references to paragraphs 4 and 5.

At this point, I gave Chat GPT an example macro (a small one, as mine does a lot of manipulation), and asked it what it recommended.

Chat GPT's first attempt:

    ' Manipulate the cross-references in newDoc
    For Each field In newDoc.Fields
        If field.Type = wdFieldRef Then
            ' Update the field code to refer to the correct paragraph in the newDoc
            field.Code.Text = Replace(field.Code.Text, "REF _Ref", "REF _Ref" & field.Result.Text)
        End If
    Next field
    
    ' Insert the manipulated text at the current cursor location in openDoc
    aRng.FormattedText = newDoc.Range.FormattedText
    
    ' Update cross-references in openDoc
    For Each field In openDoc.Fields
        If field.Type = wdFieldRef Then
            ' Update the field code to refer to the correct paragraph in openDoc
            field.Code.Text = Replace(field.Code.Text, "REF _Ref", "REF _Ref" & field.Result.Text)
        End If
    Next field
    
    ' Close the new document without saving changes
    newDoc.Close SaveChanges:=wdDoNotSaveChanges

This had the effect of breaking all references in the original document (openDoc) and all references pasted into openDoc from newDoc.

Chat GPT's second attempt (only showing one loop for newDoc, but both loops for newDoc and openDoc are the same):

    For Each field In newDoc.Fields
        If field.Type = wdFieldRef Then
            ' Update the field result to refer to the correct paragraph in newDoc
            field.Result.Text = field.Result.Text + 3 ' Adjust the number based on your specific needs
        End If
    Next field

In my actual code, I determine the number of numbered paragraphs, so I replaced "3" with that determined number. This appeared to work in both newDoc and in the pasted text for openDoc, but when I updated references, all references went back to what I show above, where the references are for paragraph 5 instead of being 4 for paragraph 5.

It seems to me that I should be able to manipulate the cross-references in newDoc (where the text is copied and manipulated prior to inserting back into the original open document). But when I simply added a number to the references in newDoc and copied this back into openDoc, the cross-references LOOKED good, but lost the added number when I updated references in openDoc.

Is there a way for me to manipulate cross-references so that they are updated once copied back into newDoc?

1

There are 1 best solutions below

1
ctviggen On

So, I've been doing more research on this. It seems like it should be easy. I insert a cross-reference to a numbered item, and I insert the reference to the paragraph number.

The cross-references to the numbered item looks like "_RefSome#". To see these, alt-F9.

These are cross-references to a "hidden" bookmark. 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. Simple, huh?

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:

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

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.