A
AlexH
In Word 2003 I need a macro to go through a document and replace any text
reference in the form "paragraph 1.1(a)" or "section 1.1(a)" with an
automatic cross-reference to the relevant paragraph (just the number needs to
be updated, not the "section", but it needs to be proceeded with section or
paragraph).
I'm a VBA noob, but with a bit of searching I found this
http://www.keyongtech.com/5263743-make-xref-from-paragraph-heading, with the
following code snippet below. There are two problems with it:
1. It only finds a reference for text that is pre-selected
2. It seems that it can't handle a full paragraph reference in the form "1.1.
(a)", as opposed to "1.1"
Any pointers on how to adapt the below to handle these points (or attack the
problem differently) would be most gratefully received!
Sub CreateXREFfromSelection()
Dim HeadingsList As Variant
Dim i, hNum As Long
Dim srchStr As Range
' What to search in headings
Set srchStr = Selection.Range
' Retrieve array of numbered items
HeadingsList = ActiveDocument.GetCrossReferenceItems(wdRefTypeNum beredItem)
' Loop over heading paragraphs
' Surround srchStr.Text with spaces to avoid finding substrings,
' e.g., 1.2 within 1.2.3
hNum = 0
For i = 1 To UBound(HeadingsList)
If InStr(HeadingsList(i), " " & srchStr.Text & " ") Then
hNum = i
Exit For
End If
Next
' Insert the cross-reference
If hNum > 0 Then
Selection.Range.InsertCrossReference _
ReferenceType:=wdRefTypeNumberedItem, _
ReferenceKind:=wdNumberNoContext, _
ReferenceItem:=hNum, _
InsertAsHyperlink:=True
Else
MsgBox ("Cross-Reference source not found")
End If
End Sub
reference in the form "paragraph 1.1(a)" or "section 1.1(a)" with an
automatic cross-reference to the relevant paragraph (just the number needs to
be updated, not the "section", but it needs to be proceeded with section or
paragraph).
I'm a VBA noob, but with a bit of searching I found this
http://www.keyongtech.com/5263743-make-xref-from-paragraph-heading, with the
following code snippet below. There are two problems with it:
1. It only finds a reference for text that is pre-selected
2. It seems that it can't handle a full paragraph reference in the form "1.1.
(a)", as opposed to "1.1"
Any pointers on how to adapt the below to handle these points (or attack the
problem differently) would be most gratefully received!
Sub CreateXREFfromSelection()
Dim HeadingsList As Variant
Dim i, hNum As Long
Dim srchStr As Range
' What to search in headings
Set srchStr = Selection.Range
' Retrieve array of numbered items
HeadingsList = ActiveDocument.GetCrossReferenceItems(wdRefTypeNum beredItem)
' Loop over heading paragraphs
' Surround srchStr.Text with spaces to avoid finding substrings,
' e.g., 1.2 within 1.2.3
hNum = 0
For i = 1 To UBound(HeadingsList)
If InStr(HeadingsList(i), " " & srchStr.Text & " ") Then
hNum = i
Exit For
End If
Next
' Insert the cross-reference
If hNum > 0 Then
Selection.Range.InsertCrossReference _
ReferenceType:=wdRefTypeNumberedItem, _
ReferenceKind:=wdNumberNoContext, _
ReferenceItem:=hNum, _
InsertAsHyperlink:=True
Else
MsgBox ("Cross-Reference source not found")
End If
End Sub