Make XREF from paragraph (heading) number

A

alex1st

Hello,

I have a large document that has a lot of section references to different
[mostly] headings (of various levels). The text is there already. I'd like to
write a macro that will replace the text itself to a cross-reference field to
the section with the same name (and alert such does not exist).

Any simple code snippet will be greatly appreciated.

Alex
 
J

Jay Freedman

Hello,

I have a large document that has a lot of section references to different
[mostly] headings (of various levels). The text is there already. I'd like to
write a macro that will replace the text itself to a cross-reference field to
the section with the same name (and alert such does not exist).

Any simple code snippet will be greatly appreciated.

Alex

Is there some way the macro could recognize exactly which pieces of plain text
should be turned into cross-reference fields? Is there some word or phrase (such
as "Section" followed by a number) that could be searched, or a specific
character style applied to the text? If there isn't anything consistent, then
the problem approaches the impossible.

The reverse process would be fairly simple: For each heading in the document,
find each occurrence of the same words in non-heading text and turn that into a
cross-reference. However, there would be no way in that scheme to find instances
of text references to headings that don't exist.
 
A

alex1st

Hello Jay,

Actually your suggestion is great - loop over all headings in the document,
find the plain text with the same number (can be a bit problematic, for
numbers like 4.2 and 4.2.1 - so search shall be more precise) and make them a
cross-reference to the heading. In my document, most of the reference are in
a column table, so I can even do a selection and search withing this
selection.
It will also solve the other issue of missing headings, since I will see the
plain text, rather than fields.

I tried to find the proper function call, but alas...

10x
 
J

Jay Freedman

The actual code you need depends critically on a number of details of the way
your document is structured, so the sample below probably won't do what you
need. If you can email an extract from the document, showing some of the
headings and some of the cross-reference locations for them, I'll try to write
some sample code tailored to it. My address is in my profile (click my name
above), or on the contacts page at http://jay-freedman.info.

Are the headings auto-numbered, rather than manually numbered? If they're
autonumbered, the document has a ListParagraphs collection that contains all the
autonumbered paragraphs. The text to search for can then be extracted from the
members of the collection. If they aren't autonumbered, then the loop will have
to search first for all the headings of the first style, and for each one search
for other occurrences of that text; then repeat for the next style, and so on.

Here's a sample of code that could work if the headings are autonumbered:

Sub demo()
Dim numpar As Paragraph
Dim i As Long
Dim srchStr As String
Dim srchRg As Range

For i = 1 To ActiveDocument.ListParagraphs.Count
Set numpar = ActiveDocument.ListParagraphs(i)
srchStr = numpar.Range.ListFormat.ListString & _
" " & numpar.Range.Text

Set srchRg = ActiveDocument.Range

With srchRg
.Find.Text = srchStr
While .Find.Execute
.MoveEnd wdCharacter, -1
.InsertCrossReference _
ReferenceType:=wdRefTypeHeading, _
ReferenceKind:=wdNumberFullContext, _
ReferenceItem:=i, _
InsertAsHyperlink:=True
.MoveUntil vbCr
.Text = ". "
.MoveUntil vbCr
.InsertCrossReference _
ReferenceType:=wdRefTypeHeading, _
ReferenceKind:=wdContentText, _
ReferenceItem:=i, _
InsertAsHyperlink:=True
.MoveUntil vbCr
Wend
End With
Next i

End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
A

alex1st

Jay,

Thank you very much. I think this will work, since my document is pretty
simple. I will send it to you off-line too with an explanation letter, after
some preliminary debugging :)

Alex
 
A

alex1st

After several discussions with Jay Freedman, the following is the code that
looks up the selected text in the document and converts it to the
cross-reference to a numbered item. The lesson is that the numbering of
cross-reference items can be retrieved only by GetCrossReferenceItems method,
which requires a Variant array (this does not appear in the on-line
reference).

So, without much ado, here is the code - enjoy

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(wdRefTypeNumberedItem)

' 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
 

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