A family of Microsoft word processing software products for creating web, email, and print documents.
no worries - thanks macropod.
Cheers
...Steve
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
________________________
A family of Microsoft word processing software products for creating web, email, and print documents.
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.
no worries - thanks macropod.
Cheers
...Steve
To prevent execution in paragraphs that aren't list-numbered paragraphs, you could add:
If .Range.ListParagraphs.Count = 0 Then Exit Sub
after the 'With Selection' line.
Regarding the bullets issue, I still think it would be better to use a bulleting style that doesn't engage the list-numbering. If you don't want to to that, you could add some code to count the number of bullet list-numbered paragraphs preceding the insertion point and adjust the 'i' value in the InsertCrossReference function accordingly. I'll leave that to you, though.
Cheers,
macropod MS MVP - Word
Hi macropod,
Re b) - yes I agree having tested it. Our original macro actually catered for that and the xref updated to the 'new' para. I think I can live with this.
What's the test to skip unnumbered paras?
Re the bullets, I didn't explain myself well (again!).
The numbering and the bullets are the normal auto numbering and bullets, not hardcoded.
The paras in the list are attached to styles, just not heading styles (although I've just tested and if you change a single heading 3 to bullet (click the bullet button), it makes the subsequent new inserted xrefs out by 1).
Cheers
...Steve
Hi Steve,
Re your 'b', that's normal behaviour if you create the new para by pressing <Enter> at the start of the old paragraph - it seems that's where Word's hidden bookmark tag is located. If you click anywhere else in the paragraph, the new cross-reference will point to the new one.
Since the macro is for list-numbered paragraphs, you really shouldn't be running it in un-numbered paragraphs. A simple test could be added to make the macro exit if it's not in a list-numbered paragraph.
As for the bullets issue, I'm not sure why that throws the macro off, but you really shouldn't be hard-formatting paragraphs that way - that's what Styles are for.
Cheers,
macropod MS MVP - Word
Ah - a dog with a bone:-)
That's pretty close, macropod (and inspirational!).
Couple of things:
a) deleting a para works as expected (error on the xref occurs)
b) inserting a new para, say BEFORE 1.1.2, the new para inherits the existing 1.1.2 xref instead of it staying with the (now 1.1.3) para.
c) running the macro in a para that has no number takes a long time (in comparison to the numbered paras which are almost instantaneous)
I've also tried it on one of my existing complex documents, and it seems to add 4 to the index (meaning it inserts the reference for the para that is 4 ahead, regardless of level). For example, in the following:
C2-1.7 Heading 2 textC2-1.7.1 Heading 3 text.
C2-1.7.2 Heading 3 text.
C2-1.7.3 Heading 3 text.
C2-1.8 Heading 2 textC2-1.8.1 Heading 3 text.:
a) Heading 3 text
C2-1.8.2 Heading 2 text
Curser in C2-1.7.2 and run macro inserts C2-1.8.1a)
Curser in C2-1.7.3 and run macro inserts C2-1.8.2
***It appears to be as a result of earlier in the document I have normal list para numbering with 4 embedded bullets:
1. Non outlined normal numbering .
2. Non outlined normal numbering .
• Bullet para.
• Bullet para.
• Bullet para.
• Bullet para.
3. Non outlined normal numbering .
4. Non outlined normal numbering .
5. Non outlined normal numbering .
6. Non outlined normal numbering .
Deleting the bulleted paras seems to bring it back into sync.
Cheers
...Steve