VBA to automatically mark cross-reference
I want to mark an entire Word document's cross-references with one click. The closest I've come to is this that marks just the selection and just level 1. It won't do subsections like 1.1(a) and it marks only the current selection. I'd like to ideally get a macro to mark all the cross references with the cross reference field in the whole document at once. Any ideas?
Sub InsertCrossRef()
Dim RefList As Variant
Dim LookUp As String
Dim Ref As String
Dim s As Integer, t As Integer
Dim i As Integer
On Error GoTo ErrExit
With Selection.Range
' discard leading blank spaces
Do While (Asc(.Text) = 32) And (.End > .Start)
.MoveStart wdCharacter
Loop
' discard trailing blank spaces, full stops and CRs
Do While ((Asc(Right(.Text, 1)) = 46) Or _
(Asc(Right(.Text, 1)) = 32) Or _
(Asc(Right(.Text, 1)) = 11) Or _
(Asc(Right(.Text, 1)) = 13)) And _
(.End > .Start)
.MoveEnd wdCharacter, -1
Loop
ErrExit:
If Len(.Text) = 0 Then
MsgBox "Please select a reference.", _
vbExclamation, "Invalid selection"
Exit Sub
End If
LookUp = .Text
End With
On Error GoTo 0
With ActiveDocument
' Use WdRefTypeHeading to retrieve Headings
RefList = .GetCrossReferenceItems(wdRefTypeNumberedItem)
For i = UBound(RefList) To 1 Step -1
Ref = Trim(RefList(i))
If InStr(1, Ref, LookUp, vbTextCompare) = 1 Then
s = InStr(2, Ref, " ")
t = InStr(2, Ref, Chr(9))
If (s = 0) Or (t = 0) Then
s = IIf(s > 0, s, t)
Else
s = IIf(s < t, s, t)
End If
If LookUp = Left(Ref, s - 1) Then Exit For
End If
Next i
If i Then
Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext, _
ReferenceItem:=CStr(i), _
InsertAsHyperlink:=True, _
IncludePosition:=False, _
SeparateNumbers:=False, _
SeparatorString:=" "
Else
MsgBox "A cross reference to """ & LookUp & """ couldn't be set" & vbCr & _
"because a paragraph with that number couldn't" & vbCr & _
"be found in the document.", _
vbInformation, "Invalid cross reference"
End If
End With
End Sub
Cross Reference entire document
Macropod, I must be misunderstanding. If the document uses Multilevel list style autonumbering:
1. Governing Law
1.1 Sub Governing Law
(a) Text is great at clause 2.
(b) Text is better at clause 2.1(a).
2. Legal Terms
2.1 Definitions
(a) Some text. In this clause refer to clause 1(b) please.
(b) Some more text.
I was assuming your code xref'd the references to "clause nnn". Sorry if I misunderstood.
I have code (not perfect) but will run through up to 4 levels of cross references which was why I was interested.
Thank you.
Quote:
Originally Posted by
macropod
The code does nothing to any Multilevel List Level numbering. Perhaps you've converted yours to plain text - in which case they're no longer using Word's Multilevel List Level numbering and they're now just plain text. Even if that were so, though, the macro would still have no effect on them unless you'd also preceded them with a space character - in other words unless you've made a real dog's breakfast of the document...
The only strings the macro might reasonably be expected to adversely affect are decimals that match existing Multilevel List Level numbers.