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. Anonymous
    2010-10-11T23:48:09+00:00

    Hi macropod,

    Just tried it on 2 other PCs at work and same result. Windows XP SP3, Word 2003 SP3. PCs are Core2Quad Intel @ 2.66Ghx with 3GB RAM.

    I can try on my PC at home tonight, which is also Windows XP SP3, Word 2003 SP3.

    Oh (added comment) - As a minimum I restart Word after a macro change, and often restart the PC as well if behaviour is particularly wierd (and once a day regardless),

    Cheers

    ...Steve

    Was this answer helpful?

    0 comments No comments
  2. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-11T23:25:35+00:00

    Hi Steve,

    All I can suggest is that Word is getting confused. Perhaps closing & re-starting Word will correct things.

    FWIW on my system the initial setup run on your test document only took about 6 seconds - and that's after I had deleted all the existing cross-reference bookmarks. You probably wouldn't want to do that deletion, since all your existing cross-references would be invalidated.


    Cheers,

    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-10-11T21:12:53+00:00

    Hi macropod,

    I deleted all the macros from the test document and therefore relied on the version of the macro that i'd stored in normal.dot.

    The first run took 15 seconds, and subsequent runs were about 3 seconds.

    It inserted "1.1" into heading 1., "1.1.2" into clause 1.1, and a single "1" in 1.12, 1.1.3, 1.1.3(a), "2.1(b)(i)" into 2.1.3, and "2.1.3(a)(ii)" into 2.2.  These were the only clauses I tested. I stopped at that point:-)

    Cheers

    ...Steve

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-10-11T05:13:36+00:00

    Interesting....

    I'll have another play - just been bombarded so may have to wait till tonight/tomorrow.

    Thanks mate!

    Cheers

    ...Steve

    Was this answer helpful?

    0 comments No comments
  5. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-11T04:56:40+00:00

    Hi Steve,

    Yes, the macro was tested on your document and it returns the correct results for me at whatever of the many locations I tested it. After the intitial setup, it takes about 2-3 seconds on my system to insert the cross-reference in your 94-page test document, with over 2300 list-numered paragraphs.


    Cheers,

    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments