Batch automation of cross-references

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
 
D

Doug Robbins - Word MVP

To get a single cross reference to a paragraph or a section of the type
1.1(a), your heading styles are going to need to be set up as

1. Level 1 Heading/Paragraph
1.1 Level 2 Heading/Paragraph
1.1(a) Heading/paragraph level 3
1.1(b) Heading/paragraph level 3, etc

which is not very commonly done as

1. Level 1 Heading/Paragraph
1.1 Level 2 Heading/Paragraph
(a) Heading/paragraph level 3
(b) Heading/paragraph level 3, etc

is far more common

If you have numbering of the second type, it is going to be very difficult
(if not impossible) to get a macro to assign the cross references as you
could have

1.1
(a)
(b)

and

1.2
(a)
(b)

and while manually, you can select any of those to be used as a cross
reference, it is done by selecting the appropriate (a) in the Insert Cross
Reference dialog whereas by using code, the hNum would be populated with
1.1(a) and there is no such item in the list of paragraphs.

For the other issue, of being able to hand more than just the selection, you
could overcome that by combining the present code with a wildcard search
routine that looked for [0-9]{1,}.[0-9]{1,}\([a-z]{1}\) using code such as
(untested);

Dim HeadingsList As Variant
Dim i as Long
Dim hNum As Long
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{1,}.[0-9]{1,}\([a-z]{1}\)",
Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindContinue, MatchCase:=False) =
True
hNum = 0
For i = 1 To UBound(HeadingsList)
If InStr(HeadingsList(i), Selection.Text) Then
hNum = i
Exit For
End If
Next i
If hNum > 0 Then
Selection.Range.InsertCrossReference _
ReferenceType:=wdRefTypeNumberedItem, _
ReferenceKind:=wdNumberNoContext, _
ReferenceItem:=hNum, _
InsertAsHyperlink:=True
End If
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top