Results 1 to 13 of 13
  1. #1
    2 Star Lounger
    Join Date
    Nov 2003
    Location
    Christchurch, Canterbury
    Posts
    122
    Thanks
    27
    Thanked 1 Time in 1 Post

    Shortcut to insert cross reference (Word XP)

    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?

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Shortcut to insert cross reference (Word XP)

    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.

    <pre>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(wdRefTypeHeadi ng)
    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
    </pre>


    Alert readers will notice this is just an adaptation of Hack #43 <img src=/S/grin.gif border=0 alt=grin width=15 height=15>.

  3. #3
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Cherrybrook, New South Wales, Australia
    Posts
    116
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Shortcut to insert cross reference (Word XP)

    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

  4. #4
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Shortcut to insert cross reference (Word XP)

    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.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Shortcut to insert cross reference (Word XP)

    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(ReferenceTyp e:=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

  6. #6
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Shortcut to insert cross reference (Word XP)

    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.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Shortcut to insert cross reference (Word XP)

    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.

  8. #8
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Shortcut to insert cross reference (Word XP)

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

  9. #9
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Shortcut to insert cross reference (Word XP)

    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.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Shortcut to insert cross reference (Word XP)

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

  11. #11
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Shortcut to insert cross reference (Word XP)

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

  12. #12
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Shortcut to insert cross reference (Word XP)

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

    <pre>sel.InsertCrossReference _
    referencetype:=wdRefTypeHeading, _
    referencekind:=wdNumberNoContext, _
    insertashyperlink:=true, _
    referenceitem:=k
    </pre>


  13. #13
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Shortcut to insert cross reference (Word XP)

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •