How do I generate a numbered paragraph Word Cross Reference field in VB?

Anonymous
2010-10-06T22:23:01+00:00

Hi There,

Does anyone know how the 9 digit unique number used in a Microsoft Word 2003 SP3 cross reference field for a numbered paragraph is derived or calculated, and what VB function I can use to extract or create it?

I use a macro (see below) that creates and inserts a cross reference field. The aim is to quickly create a cross reference from a button rather than go through the tedious "insert>reference>cross reference and select" menus.

The macro tags the reference with a letter "b" followed by an 8 digit random number (eg toggle field codes shows "{ REF b70554752 \n \h }.

The behaviour of this method of cross referencing works well most of the time, but has a couple of annoying flaws.  

Firstly, the small risk that a cross reference may be given the same number (ie the random number generator kicscks out the same number twice in a document!). Not really concerned about this as it is very unlikely to occur.

Secondly, and by far the biggest problem, is where a cross reference is created to a numbered paragraph, and the numbered paragraph is subsequently deleted. Instead of the cross reference going to "Error! - Reference Not Found" (as a 'manually inserted' cross reference does), it updates to a different, usually nearby, numbered paragraph.

On comparing the format of a 'manually inserted' cross reference field with my macro generated field, it appears that the "Microsoft" cross reference uses a 9 digit number that is somehow created uniquely based on the paragraph (for example "{ REF _Ref274201605 \n \h }"). This means that if the paragraph referred to is deleted, the reference is no longer valid and the bookmark (correctly) errors ever time.

Does anyone know how the 9 digit unique number used in a Microsoft Word 2003 SP3 cross reference field for a numbered paragraph is derived or calculated, and what VB function I can use to extract or create it?

Many thanks in advance!

Cheers

...Steve

For reference, the current macro is as follows:

___________________

Sub Add_crossRef_to_selected_para()

Dim n As Long

Dim myBookmark As String

n = Int(Rnd() * 100000000) + 1

With ActiveDocument.Bookmarks

.Add Range:=Selection.Range, Name:="b" & n

.DefaultSorting = wdSortByLocation

.ShowHidden = True

End With

Selection.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:= _

wdNumberNoContext, ReferenceItem:="b" & n, InsertAsHyperlink:=True, _

IncludePosition:=False

End Sub

________________________

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

41 answers

Sort by: Most helpful
  1. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-07T23:55:09+00:00

    Hi Steve,

    "This doesn't work the way i want it" isn't very descriptive. The code can be made much faster:

    Sub Demo()

    Dim i As Integer, xRefs, oPara As Variant

    xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)

    With Selection

      For i = 1 To UBound(xRefs)

        For Each oPara In .Range.ListParagraphs

          If .Paragraphs(1).Range = oPara.Range Then

            .InsertCrossReference ReferenceType:=wdRefTypeNumberedItem, _

            ReferenceKind:=wdNumberNoContext, ReferenceItem:=i, InsertAsHyperlink:=True, IncludePosition:=False

            i = UBound(xRefs)

            Exit For

          End If

        Next

      Next

    End With

    End Sub


    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-10-07T23:37:17+00:00

    Thanks Macropod,

    This doesn't work the way i want it either. Plus for documents with large numbers of numbered paras (eg where every para is numbered), it takes a really long time!!

    Can I email you a sample document off list?

    Cheers

    ...Steve

    Was this answer helpful?

    0 comments No comments
  3. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-07T23:21:56+00:00

    Hi Steve,

    You want to cross-reference the current paragraph to itself? If so:

    Sub Demo()

    Dim i As Integer, xRefs, oPara As Variant

    xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)

    For i = 1 To UBound(xRefs)

      For Each oPara In ActiveDocument.ListParagraphs

        If Selection.Paragraphs(1).Range = oPara.Range Then

          Selection.InsertCrossReference ReferenceType:=wdRefTypeNumberedItem, _

          ReferenceKind:=wdNumberNoContext, ReferenceItem:=i, InsertAsHyperlink:=True, IncludePosition:=False

          i = UBound(xRefs)

          Exit For

        End If

      Next

    Next

    End Sub


    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-10-07T21:16:49+00:00

    Thanks MacroPod.

    Sorry for the slow reply - I'd set the board to alert me when a reply came, but it didn't:-(

    That nearly works, thank you. I changed the ReferenceKind to be wdNumberNoContext as what I want is a cross reference  to the paragraph number.

    The problem now is that the hardcoded "1.1" needs to be dynamically replaced with whatever the paragraph number is where my curser is. Any advice on that? This was the challenge that led to the development of the orginal way of doing it. If I can resolve that issue then I think we have a winner:-))

    Thanks in advance!

    Cheers

    ...Steve

    Was this answer helpful?

    0 comments No comments
  5. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-07T00:27:35+00:00

    Hi Vidar,

    That's not how you go about inserting the cross-reference. The 9-digit # is essentially random and is allocated by Word. The following code shows how you'd insert a cross-reference at the selection point of the page # on which numbered paragraph 1.1 appears:

    Sub Demo()

    Dim i As Integer, xRefs As Variant

    xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)

    For i = 1 To UBound(xRefs)

      If Split(Trim(xRefs(i)), " ")(0) = "1.1" Then

        Selection.InsertCrossReference _

        ReferenceType:=wdRefTypeNumberedItem, _

        ReferenceKind:=wdPageNumber, ReferenceItem:=i, _

        InsertAsHyperlink:=True, IncludePosition:=False

        Exit For

      End If

    Next i

    End Sub


    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments