Group: microsoft.public.word.vba.general
From: c.dan.sherman@gmail.com
Date: Friday, March 14, 2008 4:16 PM
Subject: Re: Word 2007 InsertCrossReference wrong number?

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.

On Mar 14, 10:14=A0am, c.dan.sher...@gmail.com wrote:
> 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. =A0I 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. =A0I also have a macro to change
> a manual cross reference into a REF field (see below) instead of going
> through the CrossReference dialog everytime. =A0It works on Word 2000
> and 2003, and almost works in 2007. =A0In 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 =3D Len(Selection.Text)
> strTemp =3D Selection.Text
> myHeadings =3D ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
> If l > 0 Then
> For i =3D 1 To UBound(myHeadings)
> =A0 =A0 If Left$(LTrim(myHeadings(i)), l) =3D strTemp Then
> =A0 =A0 =A0 =A0 Selection.InsertCrossReference _
> =A0 =A0 =A0 =A0 =A0 =A0 ReferenceType:=3D"Heading", _
> =A0 =A0 =A0 =A0 =A0 =A0 ReferenceKind:=3DwdNumberNoContext, _
> =A0 =A0 =A0 =A0 =A0 =A0 ReferenceItem:=3Di, _
> =A0 =A0 =A0 =A0 =A0 =A0 InsertAsHyperlink:=3DFalse, _
> =A0 =A0 =A0 =A0 =A0 =A0 IncludePosition:=3DFalse
> =A0 =A0 =A0 =A0 End
> =A0 =A0 End If
> Next i
> MsgBox ("Could not find target paragraph. =A0Check your references.")
> End If
> End Sub