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: Oldest
  1. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-12T00:56:56+00:00

    Hi Steve,

    As an experiment, I re-opened your test document & used the macro to insert a couple of cross-references. All worked as they should, in both Word 2000 and Word 2007.


    Cheers,

    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-10-12T01:33:34+00:00

    Damn!

    Would you mind emailing your normal.dot and test doument to me at *** Email address is removed for privacy *** - maybe there's something quirky in my normal.dot or somethin?

    Cheers

    ...Steve

    Was this answer helpful?

    0 comments No comments
  3. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-12T03:16:29+00:00

    Hi Steve,

    These forums automatically remove email addresses, so I can't see your's to respond to. Even so, the most you should need to do is to exit Word, temporarily rename your Normal.dot to, say, AbNormal.dot, then re-start Word.

    The other possibility is that some corruption has crept into your document. A simple fix for that in most cases is to insert a new last paragraph into the document, then copy & paste all except that new paragraph into a new document. If you copy the code over as well, you should have a fairly 'clean' document to resume testing with.

    I've also thought of a way to make the code execute almost instantaneously for any list-numbered paragraphs that already have a bookmark ID. I should have realised this before :(! It consists of nothing more complicated than testing for the presence of a bookmarkID for the selected paragraph, which is accomplished by the addition of an If test before the 'On Error Resume Next' line and closing it after the 'Next' line.

    In effect, this change bypasses all the code inside the IF test and makes the macro work as one might if it could be assumed that all list-numbered paragraphs had bookmarkIDs pre-allocated. If you still get errors with this change, it seems certain there's something awry with your document and/or Word installation.


    Cheers,

    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-10-12T21:48:13+00:00

    hi macropod,

    Unfortunately, I didn't get a chance to try it at home last night.

    The new macro is certainly fast, and works pretty well (but not entirely correct) on a brand new document started from scratch.

    It seems to not work (ie inserting incorrect cross refs when run, albeit the same wrong xref in any particular para) on existing documents (including the test document, which I've recreated from scratch after creating a new clean normal.dot).

    It also goes a bit off when paras are inserted or deleted (even on the brand new document).

    I really appreciate the huge amount of thought and effort you are putting in to this issue! But I'm thinking that I've wasted enough of your time and should just live with the warts of the original macro.

    Having said that, I'm delighted to keep testing if you are really keen to get it working! I just don't want to take (too much!) advantage of your generosity.

    Cheers

    ...Steve

     PS the email address I posted I can see in the discussion, however I'll try and do it in a non-email format:

    spjunk at vidar dot com dot au

    Was this answer helpful?

    0 comments No comments
  5. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2010-10-13T01:33:40+00:00

    Hi Steve,

    OK, I've figured out what the problem was. The direct BookmarkID approach only works if the BookmarkIDs were all allocated in order to begin with, which is what I had done - you'll recall that I mentioned deleting all your bookmarks, then re-allocating from scratch to time a worst-case scenario. Where the BookmarkIDs may not all be allocated in order to begin with, a bit of circumlocution is required. Change the 'InsertCrossReference' lines to:

      .InsertCrossReference ReferenceType:=wdRefTypeBookmark, ReferenceKind:=wdNumberFullContext, _

        ReferenceItem:=ActiveDocument.Bookmarks.Item(.Paragraphs.First.Range.BookmarkID - 1).Name, _

        InsertAsHyperlink:=True, IncludePosition:=False


    Cheers,

    macropod MS MVP - Word

    Was this answer helpful?

    0 comments No comments