We have documents with a situation where there are multiple (stacked) endnote markers, footnotes, and cross-references. Such as "Stacked[5a63]" (The characters between square brackets are superScript) See attached Word doc.
And the "3" is a cross-reference, this is the character I need to move.
We need to get the cross-references to be in numerical order with the endnotes.
In this script, I have found where the cross-references are, and if they are preceded by an endnote with a larger number. So now, I just need to figure out how to move the cross-reference before the endnote marker.
Ideally, without using anything like cut/paste, as we have had problems using that on large documents.
Here's my code where I'm trying to figure this out. If you run this, you should see my print statements where I print "swap here..." but I don't know how to do that part.
Code:
Sub bhh_stackedSourcesFix()
' Written by Brian Hoard, www.BrianHoard.com
' Script date: 11/16/2022
' Script to correct citation numbers when multiple citations are stacked next to each other.
Dim scriptName As String
scriptName = "bhh_stackedSourcesFix v0.1"
Dim bhhUndo As UndoRecord ' Allows grouping this entire script within a single undo action.
Application.ScreenUpdating = False
' Begin undo record
Set bhhUndo = Application.UndoRecord
bhhUndo.StartCustomRecord (scriptName)
' Check if we're dealing with dynamic endnotes, and process accordingly.
If ActiveDocument.Endnotes.Count > 0 Then
Debug.Print ("Endnotes are in use.")
' Looking for cross-refs which may be out of order.
Dim oStory As Range
Dim i As Integer
Dim XrefNum As Integer
Dim rng_Xref As Range
Dim rng_charCheck As Range ' range to check each character left of Xref.
Dim rng_charCheck2 As Range
Dim str_charCheck As String
Dim str_charCheck2 As String
Dim eNote As Endnote
Dim lng_eNoteStart As Long
Dim int_endnoteMarker As Integer
Dim j As Integer
For Each oStory In ActiveDocument.StoryRanges
For i = oStory.Fields.Count To 1 Step -1
If oStory.Fields(i).Type = wdFieldNoteRef Then
' We found a cross-reference.
Debug.Print ("----------")
Debug.Print ("Fields(" & i & ") is a cross-ref.")
Set rng_Xref = oStory.Fields(i).Result ' stores the cross ref range
' Okay, we have our cross-ref character(s),
' but are they superscript numbers?
str_charCheck = rng_Xref.Text
If (rng_Xref.Font.Superscript = True) And (IsNumeric(str_charCheck) = True) Then
Debug.Print (" rng_Xref is a numeric superscript, " & str_charCheck)
' Our cross-ref is a numeric superscript. Next, check if there
' are any chars, cross-refs, or dynamic endnotes
' preceding it that are out of order with current cross-ref.
Set rng_charCheck2 = rng_Xref.Duplicate ' Copy rng_Xref so we leave it untouched as we test preceding chars.
rng_charCheck2.MoveStart wdCharacter, Count:=-1 ' Move range to left 1 char.
Debug.Print ("rng_charCheck2.Start, .End: " & rng_charCheck2.Start & ", " & rng_charCheck2.End)
str_charCheck2 = rng_charCheck2.Characters(1)
STXcheck:
Debug.Print ("STXcheck:")
If str_charCheck2 = Chr(2) Then ' Hidden STX symbol,
Debug.Print ("STX character found, checking if it's an endnote.")
Debug.Print ("rng_charCheck2.Start: " & rng_charCheck2.Start)
' Check for endnote here
' Loop through endnotes, starting at highest one, working backwards.
For j = ActiveDocument.Endnotes.Count To 1 Step -1
Set eNote = ActiveDocument.Endnotes(j)
With eNote
If .Reference.Start = rng_charCheck2.Start Then
Debug.Print (".Reference.Start: " & .Reference.Start)
' Our preceding char is an endnote.
int_endnoteMarker = eNote.Index ' Store endnote marker number
Debug.Print ("endnote marker: " & int_endnoteMarker)
If int_endnoteMarker > str_charCheck Then
Debug.Print ("int_endnoteMarker: " & int_endnoteMarker & " is > str_charCheck: " & str_charCheck)
Debug.Print ("Swap here...")
Else
Debug.Print ("int_endnoteMarker: " & int_endnoteMarker & " is NOT > str_charCheck: " & str_charCheck & ", we're done.")
End If ' int_endnoteMarker > ...
End If ' endnote start = rng_charCheck2.Start
End With ' eNote
Next j
rng_charCheck2.MoveStart wdCharacter, Count:=-1
str_charCheck2 = rng_charCheck2.Characters(1)
GoTo STXcheck
Else
Debug.Print ("Not STX")
End If ' STX character
Else
Debug.Print ("rng_Xref is NOT a numeric superscript.")
End If ' rng_Xref is superscript number or not.
' Check if the character preceding cross-ref is a superScript number,
' if so, does it need swapped?
Set rng_charCheck = rng_Xref ' Copy range to start our check for the character preceding.
rng_charCheck.Start = (rng_Xref.Start - 1)
' Check if this character is a superScript number.
str_charCheck = rng_charCheck.Characters(1)
End If ' /field type cross-ref
Next i
Next oStory
Else
Debug.Print ("No Endnotes are in use.")
End If ' /endnotes.Count > 0 or not.
bhhUndo.EndCustomRecord ' End undo block
Exit Sub
End Sub ' bhh_stackedSourcesFix v0.1