• Shortcut to insert cross reference (Word XP)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Shortcut to insert cross reference (Word XP)

    Author
    Topic
    #419720

    One of my clients has huge documents – 100s of headings – when they go to insert a cross reference they are grumpy about scrolling through all the headings. Any simple way of getting to (say) 10.3.2 in the insert cross reference box?

    Viewing 1 reply thread
    Author
    Replies
    • #948602

      The Cross-Reference dialog is very buggy when you try to work with it from VBA (I’ve talked about a bit in other posts recently, actually). I’d personally suggest staying away from anything more complicated than using the .Show method. I got so fed up with it with in my own templates that I built an entirely new dialog, just to have something easier to work with. But I doubt you’re interested in taking things that far.

      Given the initial condition that your client already knows what they want to reference (10.3.2 for example), would it be acceptable for them to type that in literally, then select it and run a macro? (from a keybinding or the shortcut menu). If so, the following will try to match the selected text to a heading number, and if found, replace the selection with a cross reference to that heading. Your example didn’t include any trailing period on the number; if your actual document does have a trailing period, you’ll need to modify the code slightly.

      In the interest of simplicity, this code assumes the selected text is just the number — no leading or trailing spaces. It’s fairly easy to collapse the selection first, just ask if you’d like a pointer.

      Sub ReplaceSelectionWithXRef()
      Dim vHeadings As Variant
      Dim v As Variant
      Dim k As Integer
      Dim sel As Selection
      
      Set sel = Selection
      vHeadings = sel.Document.GetCrossReferenceItems(wdRefTypeHeading)
      k = 1
      For Each v In vHeadings
          If Split(Trim(v))(0) = sel.Text Then
              sel.InsertCrossReference _
                referencetype:=wdRefTypeHeading, _
                referencekind:=wdNumberNoContext, _
                referenceitem:=k
              Exit Sub
          End If
          k = k + 1
      Next v
      MsgBox "Couldn't match: " & Selection.Text
      End Sub
      

      Alert readers will notice this is just an adaptation of Hack #43 grin.

      • #967935

        What I need to do is create a table in a document that in column 1 lists all the heading numbers and in column 2 shows the matching heading text.

        Ideally I would like to automatically create those columns which of course mirror the Contents. However to get the show on the road I can type in the numbers for column 1 and then use the macro that Andrew Savikas has hacked (his words).

        Does anyone have any ideas please for

        1] Automatically populating the table (for those curious it will have two further columns to which various document refererences will be manually entered)

        2] Inserting the matching heading text.

        Thanks.

        • #968091

          Try this. The insertion point should be where you want the table.

          Sub ListHeadingsInTable()
          Dim vHeadings As Variant
          Dim v As Variant
          Dim i As Integer
          Dim sel As Selection

          ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=2
          vHeadings = ActiveDocument.GetCrossReferenceItems(ReferenceType:=wdRefTypeHeading)
          For i = 1 To UBound(vHeadings)
          Selection.InsertCrossReference ReferenceType:=wdRefTypeHeading, _
          ReferenceKind:=wdNumberFullContext, ReferenceItem:=i
          Selection.MoveRight Unit:=wdCell
          Selection.InsertCrossReference ReferenceType:=wdRefTypeHeading, _
          ReferenceKind:=wdContentText, ReferenceItem:=i
          If i < UBound(vHeadings) Then
          Selection.MoveRight Unit:=wdCell
          End If
          Next i
          End Sub

          • #968139

            Hans that is just so cool your fingers must have frozen.

            Two things happen when running the macro. After every so many headings (about 16) a warning comes up “Word has insufficient memory etc” but the Yes option continues the macro so it is not a real problem.

            After inserting the last item instead of closing the standard Run Time error 4198 Command failed message appears. Again it is not a real problem so I only come back in case it is one of those easy things to fix.

            Bottom line though is it does the job so thank you, once again.

            • #968142

              I haven’t tested the code on long documents, only on two short documents created for this purpose only. It ran without problems there. Since Word stores all modifications to a document in the undo buffer, even when executing a macro, it is quite possible that Word has to flush the undo buffer from time to time while running the macro on a long document with lots of heading paragraphs. I don’t know where the 4198 error comes from, though.

            • #968146

              Thanks for coming back so quickly. Neither issue prevents the macro from doing its job which is what really matters.

            • #968156

              Hans

              I was singing your praises as your macro is saving me a lot of time. One of our developers who wouldn’t know the Word commands heard me and took a look and saw what causes the runtime error.

              He inserted a little bit of code at the end after the first line below.

              Selection.MoveRight Unit:=wdCell
              Else
              Exit Sub
              End If
              Next i
              End Sub

              Hope this helps anyone else who may find Hans’ macro useful.

            • #968162

              I’m not sure why that makes a difference, but if it helps, who cares. Glad you have a better-running macro now.

      • #968718

        Andrew,
        Your macro is a great timesaver. How can I modify the code so that the cross-reference gets inserted as a hyperlink?

        • #968727

          If you search the VBA help for “InsertCrossReference”, the first result gives you your answer.

          sel.InsertCrossReference _
                    referencetype:=wdRefTypeHeading, _
                    referencekind:=wdNumberNoContext, _
                    insertashyperlink:=true, _
                    referenceitem:=k
          
          • #968734

            Andrew,
            Thanks for the quick response. Using your macro is much easier than mucking around with the Cross-reference dialog!

    • #948605

      Judith,
      I also get frustrated when using the x-ref dialogue box. My cross refernces usually take the form of ‘Refer to “10.2.6 How to do this” on page 59’ To put this in needs 3 uses of the dialogue box – heading number, heading text and page number.
      This is what I do to save a little time:
      1) Add a button on a toolbar so I don’t have to navigate through all the menus to get to insert x-ref.
      2) With the cursor between the inverted commas, I insert the x-ref to the heading number by scrolling down. Without closing the dialogue box, I insert the heading text and then the page number. (If you close the dialogue box, you have to scroll down and find to the heading again!)
      That gives me ‘Refer to “10.2.6How to do this59″‘, to which I then space out as necessary.

      By the way Judith, isn’t NZ “DownUnder and EAST a bit”?
      Regards,
      Judith

    Viewing 1 reply thread
    Reply To: Shortcut to insert cross reference (Word XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: