InsertCrossReference issue with SEQ fields RRS feed

  • Question

  • I use an SEQ field in my documents to number items.  I need to then insert cross-references to those SEQ fields.  Currently, I do this manually by inserting a cross reference to “SEQ_ID” (SEQ_ID is a custom label that I created via the insert caption dialog earlier).  I wish to automate both aspects of this process.

    I have generated two procedures.  The first inserts a SEQ field with a bookmark of SEQ_ID.  This first procedure seems to work without any issues at all.

    Sub Insert_SEQ_Number()

    With Selection


        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="SEQ SEQ_ID\* Arabic \* MERGEFORMAT", PreserveFormatting:=True

        .TypeText Text:="." & vbTab

    End With


    End Sub

    The second procedure inserts the cross-reference to a specified SEQ_ID item (specified by “x”).  “x” is an integer that corresponds to the SEQ_ID number that I want to cross-reference.  I add a Caption Label for SEQ_ID at the start in case one does not already exist in the CaptionLabels collection (other users may not have added it yet, and it seems like there is no consequence to attempting to re-add the label if it already exists).

    Sub Insert_SEQ_Ref(x as integer)

        CaptionLabels.Add Name:="SEQ_ID"

        Selection.InsertCrossReference ReferenceType:="SEQ_ID", ReferenceKind:= wdOnlyLabelAndNumber, ReferenceItem:=x, Include "


    End Sub

    (I plan to include a check to make sure that x does not exceed the number of SEQ_ID fields in the document, don’t worry).

    The problem is that the cross-reference that is inserted by the above procedure includes not just the referenced SEQ_ID field value, but all the text between it and the next SEQ_ID field (including the next SEQ field value) to follow the referenced SEQ field.  If there is no SEQ_ID field between the referenced SEQ_ID field and the cross-reference insertion point, the procedure crashes out—presumably because the cross-reference is trying to include itself in the cross-reference text.

    The weird part is that when I record a macro of the manual insertion process, the InsertCrossReference statement looks identical to the one above (except that “x” is replaced by a string indicating the number value of the selected SEQ_ID item).

    I think that I am specifying the ReferenceItem incorrectly, but I don’t know how to fix this.  I’ve tried specifying it by number index and by item name (as returned by the GetCrossReferenceItems method), but to no avail.

    Thoughts?  Ideas?

    • Moved by Bill_Stewart Saturday, June 2, 2012 2:42 PM Moving to more appropriate forum (From:The Official Scripting Guys Forum!)
    Friday, June 1, 2012 10:47 PM

All replies

  • Good explanartion but you are in the wrong forum.  You want the Office VBA Developers Forum.

    I am not trying to put you off but the question is pronbabaly beyond most admin scripters and I don't know the answer off the top of my head.

    I am also certain that without the rest of your code and a sample of the document that it would be very hard to figure out why this is happening.

    It sounds ot me like you are trying to create numbered bookmarks that can be indexed byy telling the indexer to finf the fields that fit that pattern.

    I am not sure this is possible.  You may have to enumerate the book marks and manually generate the index entries.  Just a thought.


    • Edited by jrv Saturday, June 2, 2012 12:12 AM
    Saturday, June 2, 2012 12:04 AM
  • Thanks for the tip--I will repost over in Office VBA.

    I have to think it can be done completely in VBA--I can do everything except selecting the field I want using VBA.  That last step is what's killing me.

    Also, the code I provided is just about all that there is--it is a very minimal VBA procedural set.  I'll see what they say over in VBA.


    Saturday, June 2, 2012 4:32 AM
  • Now that the thread is moved I have reread it and I really don't understand what you are trying to do.

    Are you trying to index these items?  Are you trying to build a cross-recfreence or are you trying to create a merge documents?  Your explanation is very confusing.

    I even read it liek you are trying to add a link beterrn two iesm.

    A link can refernce a bbokmark or other items.  A cross-refrence is a generated table in Word similar to an index.  You can cross refrence by many items including numbered items and bookmarks.


    • Edited by jrv Saturday, June 2, 2012 6:31 PM
    Saturday, June 2, 2012 6:28 PM
  • Sorry if the explanation was confusing.  Here's what I am trying to do:

    1) Use SEQ fields to number a list of items.

    2) Within the list, I want to cross-reference to SEQ values of items elsewhere in the list.

    Manually, it is quite simple--I place the SEQ field with the following code:

    {SEQ SEQ_ID \* Arabic \* MERGEFORMAT }

    ...into the document.  I also create a Caption Label called "SEQ_ID" (making sure to specify "Exclude Label from Caption"), but this ends up being stored in the and only needs to be done once on a computer.  To create my cross-references, I open the Insert Cross References dialog box, select "SEQ_ID" from the drop down for the type of cross-reference, and then select the particular SEQ_ID entry I wish to cross-reference to from the list of all SEQ_IDs displayed.  I have to specify "Label and Number Only" (since the caption excludes the Lable, this simplifies to just the number).

    I do not use bookmarks since the ordering of the SEQ_ID fields sometimes gets shuffled around a bit, and there would be no guarantee that the bookmark name would actually match up with the numbering value provided by the SEQ_ID field.


    Monday, June 4, 2012 4:36 AM
  • That exaplins technically the steps you are taking.  I am asking what it is that youare trying to accomplish.  WHat is the end product?  Are you tring to create a cross refrence na index or a custom numbered list or are you trying to do something else?


    Monday, June 4, 2012 1:48 PM
  • I am trying to create a custom numbered list where I can refer back to earlier items in the list.  For example, a list of steps in an instruction manual, where, for example, step 3) might include a cross-reference back to step 2).

    Tuesday, June 5, 2012 5:52 AM
  • I tried you code but it does not appear to work.  It does not find the numbered crossreferences and the bookmarks do not appear to be created.

    I have notfound why this is happening.  Perhaps someome else here has done something similar.

    Crossrefernces are usually used to be indexes and tables so there is not much documentation or many examples of this.


    Tuesday, June 5, 2012 7:32 AM
  • As far as I can tell, the problem here is that the referencing mechanism used by Word expects to see the caption label before the sequence number. If it doesn't, it just doesn't work as you would hope. I wondered if inserting "SEQ_ID" then hiding it might work, but it does not.

    In this case, if you are doing this for yourself, you could do a lot worse than this:

     a. create your own bookmark for each list item that you want to reference. The bookmark needs to cover the { SEQ } field. Let's call it "mybm"

     b. reference that bookmark via { REF mybm }

    If you need to package that up for someone else to use, I think you would have to create your own UserForm that did much the same thing as the normal Cross-Reference dialog box (list the relevant paragraphs, allow selection, generate a bookmark name, insert it to cover the SEQ, then insert the { REF } field)

    Peter Jamieson

    Thursday, June 14, 2012 9:28 AM