Word 2007 InsertCrossReference wrong number?

C

c.dan.sherman

I'm going through many (hundreds if ont thousands) of documents that
have hand numbered paragraphs and cross-references and converting them
to autonumbered paragraphs and references. I wrote a macro to convert
the paragraphs to numbered heading styles, it works great on every
version of word that I've tested it on. I also have a macro to change
a manual cross reference into a REF field (see below) instead of going
through the CrossReference dialog everytime. It works on Word 2000
and 2003, and almost works in 2007. In 2007 it references the wrong
number (2 items back, i.e. 4.3 becomes 4.1).

I used the debugger to step through and found that it finds the
correct reference, but doesn't insert it correctly.


Sub AutoCrossReference()
Dim myHeadings() As String
Dim strTemp As String
Dim l As Long
Dim i As Long
l = Len(Selection.Text)
strTemp = Selection.Text
myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
If l > 0 Then
For i = 1 To UBound(myHeadings)
If Left$(LTrim(myHeadings(i)), l) = strTemp Then
Selection.InsertCrossReference _
ReferenceType:="Heading", _
ReferenceKind:=wdNumberNoContext, _
ReferenceItem:=i, _
InsertAsHyperlink:=False, _
IncludePosition:=False
End
End If
Next i
MsgBox ("Could not find target paragraph. Check your references.")
End If
End Sub
 
C

c.dan.sherman

I think I've found the problem. The documents have changes tracked,
and a deleted item was throwing everything off. I still need to
figure out why, but at least I'm getting closer.
 
S

Stefan Blom

Note that, in Word, you can never be certain that numbering is correct until
you have accepted (or rejected) all changes in a document. This applies to
all kinds of automatic numbering.

--
Stefan Blom
Microsoft Word MVP


I think I've found the problem. The documents have changes tracked,
and a deleted item was throwing everything off. I still need to
figure out why, but at least I'm getting closer.
 
D

dan.g

For some reason if I get NumberedItems instead of Headings it works
just fine.
Hope that helps someone.
 

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