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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

    Author
    Topic
    #374430

    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

    Viewing 1 reply thread
    Author
    Replies
    • #605764

      Troy,

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

      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
      

      Gary

      • #605792

        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

        • #605795

          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

        • #606018

          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

        • #607677

          Could you do something with the array returned by ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)? 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

          • #607853

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

            Thanks anyway!!
            Troy

    • #607777

      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.

      • #607788

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

        Thanks for the idea!!
        Troy

    Viewing 1 reply thread
    Reply To: Adding crossreferences for all Heading 4s in Range (VBA Word 2000)

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

    Your information: