ListNum cross-reference macro

B

bha281

I would like to create a macro that will: (1) place a LISTNUM field at
the insertion point; (2) move the insertion point to the end of the
document; and (3) insert a cross-reference to the page number of the
LISTNUM field created in step one. Steps 1 and 2 I've already figured
out after some trial and error with recording macros, but I don't know
what I need to do to make step 3 work.

Sub Crosslist()
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"LISTNUM ", PreserveFormatting:=False
Selection.EndKey Unit:=wdStory
Selection.TypeText Text:="Here's where I get stuck."
End Sub

Any advice (or indications that this is an exercise in futility) would
be appreciated.
 
D

Doug Robbins - Word MVP

This should do what you want:

Dim myfield As Field
Dim i As Long
Dim refrange As Range
On Error GoTo CreateVar
i = ActiveDocument.Variables("sequence").Value + 1
Continue:
Set myfield = ActiveDocument.Fields.Add(Range:=Selection.Range,
Type:=wdFieldEmpty, _
Text:="LISTNUM")
myfield.Select
ActiveDocument.Bookmarks.Add "List" & i, Selection.Range
Set refrange = ActiveDocument.Range
refrange.Collapse wdCollapseEnd
ActiveDocument.Fields.Add Range:=refrange, Type:=wdFieldEmpty, _
Text:="Ref List" & i
Exit Sub

CreateVar:
If Err.Number = 5825 Then
ActiveDocument.Variables("sequence").Value = 1
GoTo Continue
End If



--
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
 

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