Results 1 to 9 of 9
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    I'm thinking up several conveluted ways of doing this, but I'm guessing there is an easier way.

    I need to add crossreferences at a certain place (can use a bookmark) for all Heading 4s in a certain range.

    I can set the range and the starting point for where to add the crossreferences. What I can't think of a reasonable way to do is to only add crossreferences to the Heading 4s in that range.

    Just give me a nudge in the right direction and I can take it from there.

    Thanks for your ideas!!
    Troy

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Troy,

    Hard to see how you can avoid looping through all the paragraphs in the range:

    <pre>Dim rngDefinedRange As Range
    Dim aPara As Paragraph

    For Each aPara In rngDefinedRange
    If aPara.Style = "Heading 4" Then
    'insert x-ref in appropriate part of aPara.Range
    End If
    Next 'aPara
    </pre>

    Gary

  3. #3
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Gary,
    Yes, I can see how looping would work. However, how would I insert the cross-reference to the heading once it is found? The code to insert the crossreference for the heading requires that I know the index within the HeadingStyles collection. If I could determine that, I'd be home free. This is the code I am talking about:

    Selection.InsertCrossReference ReferenceType:="Heading", ReferenceKind:= _
    wdContentText, ReferenceItem:="45", InsertAsHyperlink:=True, _
    IncludePosition:=False

    Getting that elusive "ReferenceItem=" is my main problem. If I had a way to do that, I could loop or even do finds for Heading 4s and accomplish my purpose.

    Thanks again for your help!!
    Troy

  4. #4
    New Lounger
    Join Date
    Aug 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Hi Troy and Gary

    Troy replied to your posting with a solution to finding the Heading 4's, this option will work but I think if the range is overy 10 pages it will take some time to execute. I see you have a further question regarding the Cross References I do not have a solution for that to hand however below is an altenative for locating each of the Heading 4's. To anyone reading if there is a better way I would appreciate hearing from you.

    Roanna

    Dim fndFind As Find
    Dim rngDefinedRange As Range
    Dim rngFoundRange As Range
    Set fndFind = rngDefinedRange.Find
    With fndFind
    .Style = "Heading 4"
    .Forward = True
    .Execute
    If .Found = True Then
    Do Until .Found = False
    Set rngFoundRange = .Parent
    With rngFoundRange
    'insert x-ref in appropriate part of rngFoundRange
    'collapse range
    .Parent.Collapse wdCollapseEnd
    .Execute
    Loop
    End If
    End With

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Troy,

    Sorry I didn't get that the first time - that definitely is a tough one. The cross-reference facility is an impressive utility built into Word - somehow it easily assembles an indexed list of the Heading styles collection as you put it - just don't know an efficient way to approximate building that list.
    One thought that comes to mind is to play around with the dialog itself:

    With Dialogs(wdDialogInsertCrossReference) - ".ReferenceItem" seems to be the property we're after, but can't figure out how to extract that property from the dialog - the dialog highlights the first item in the list every time you open the dialog (rather than say the item that's currently selected in the document, the way the InsertBookmark dialog works) - so this might be a dead end too.

    You could try using Find to locate every Heading4 paragraph in the document, and when it finds one, insert a bookmark, the name of which is the paragraph's index number in the document's "Heading 4 paragraphs collection". Then when your code cycles through all the Heading4 paragraphs in the required range, it could look for the bookmark contained in that paragraph, extract the bookmark name, and use that for the needed index when inserting the cross-reference.
    A lot of work....

    Gary

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Could you do something with the array returned by <font color=blue>ActiveDocument.GetCrossReferenceItems(w dRefTypeHeading)</font color=blue>? I've used this with captions before, but never with headings and it would take quite a bit of playing that I don't have time for at the moment.

    StuartR

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Just to provide a left-field way of thinking about this - have you considered a TOC. That is after all a collection of cross references.

    If your TOC field looks like this {TOC t "Heading 4,1" b "Bookmark"} then it will contain all of your Heading 4's contained in the range of text described by the bookmark Bookmark. Another switch is available to hide the page numbers if you don't want them.

    If this is too simplistic, you may also be able to reveal the codes within that TOC (not the entire TOC itself). When you do, you will see that each heading is described by a hidden bookmark name that is used internally within Word to cross refer to. If you can harvest these then you will have all the links you require.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Andrew,
    I like the way you think!! I think this could work.

    Thanks for the idea!!
    Troy

  9. #9
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    I've played with this some, but never been able to extract just the Heading 4s from it.

    Thanks anyway!!
    Troy

Posting Permissions

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