• Adding all headings to a list box (VBA for Word 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Adding all headings to a list box (VBA for Word 2000)

    Author
    Topic
    #360419

    I’m in learning mode and can’t quite connect the dots of what I know for a solution. I want to search through a document and put all the headings into a list box. This exact thing is done in the Cross-reference dialog box (on the Insert menu, click Cross-reference; in the Reference type list, click Headings. The For which headings list appears. I just can’t figure out how to duplicate that.

    Thanks for your help!!
    Troy

    Viewing 0 reply threads
    Author
    Replies
    • #542731

      I looked in the help file (Word 97) and may have hit the jackpot with this line of code that is supposed to return an array

      myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

      Look at the help file for the term GetCrossReferenceItems

      If you still can’t work it out and wanted to iterate through the text to populate the list then the following will move to the next heading from whereever the selection currently is placed.

      Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, count:=1, Name:=""
      • #542830

        Thanks for your reply. I found the statement:

        myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

        but could never figure out how to use it properly to collect all the headings and put them in a list. I will need to select a heading and then perform some functions on it afterward. I probably am just lacking the syntax to do this properly. I’m very much a newbie when it come to arrays as well. This may be part of my problem.

        Regarding:

        Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, count:=1, Name:=””

        I still need to understand what to do to populate my list box. I would also need to make sure I could find it again later to perform the functions I need to on what I select.

        Thanks again for your help!!
        Troy

        • #542845

          Troy

          This will work to populate the list if it is located in your form initialise macro (or behind a button click on the form)

          Dim myHeadings as Variant
          myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
          Me.ListBox1.List = myHeadings

          I am also on shaky ground when playing with Arrays and I have not defined the Array correctly so some memory could be saved by correct dimensioning.

          When you select an entry, what did you plan to do with that knowledge? Your functions after that may require different inputs such as list position / text / number – all of which can be worked out from the selection.

          • #542868

            Thanks again for your response. Unfortunately, that code doesn’t seem to do anything.

            Also, to answer your question, the main thing I need to do after I select a heading and click OK is to find that particular heading. Once found I will format the heading with a specific style, adjust numbering, etc. If I can just populate the list, select the heading and then find it, I can take it from there.

            Thanks!!
            Troy

            • #542875

              Maybe you will have to enumerate the array to assign it to the listbox. That command does at least create the array, even if it is a very odd beast:

              Sub ListHeadings()
              Dim myHeadings As Variant, i As Integer
              myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
              For i = 1 To UBound(myHeadings)
                  Debug.Print myHeadings(i)  'change this to assigning it into the ListBox
              Next
              End Sub
            • #542887

              This is what I am trying, but still I get nothing:

              Private Sub frmAppendixStartsat_Initialize()
              Dim DocumentHeadings As Variant, i As Integer
              DocumentHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
              For i = 1 To UBound(DocumentHeadings)
              lbAppxHeadings.AddItem DocumentHeadings(i)
              Next i
              End Sub

              What am I missing?

              Thanks again!!
              Troy

            • #542930

              Curious. I copied your code into a userform with a listbox on it and it works perfectly. Are you getting any errors at all? If not, are there definitely headings in the active document?

            • #542977

              Pardon my stepping in here… hello

              Change the name of your procedure to Userform_Initialize, and it will work.

              The reason you weren’t getting anything in the listbox is that the procedure frmAppendixStartsat_Initialize() never executed. For reasons that aren’t clear to me, shrug Microsoft decreed that all events that belong to a userform — any userform, regardless of its name — are called Userform_XXXX(), not ActualUserformName_XXXX().

            • #543077

              Yes!! You were exactly right. I changed this in one of my fruitless attempts to get my code to work and forgot to change it back. It works fine now.

              However, now that my list box contains the lists, I cannot seem to do the next step. I want to find (and select) the selected item (heading) in the list after I click OK. I have tried the following code, but it does not seem to work.

              Private Sub cmbOK_Click()
              Dim i As Integer
              For i = lbAppxHeadings.ListCount – 1 To 0 Step -1
              If lbAppxHeadings.Selected(i) = True Then
              Selection.GoTo What:=wdGoToHeading, Which:=wdGoToAbsolute, Count:=i
              Exit For
              End If
              Next i
              End Sub

              Thanks for any help you can give!!
              Troy

            • #543084

              Without testing this, I’m guessing that the GoTo is moving forward (or back) from the current selection point. If that is the case you should move the insertion point back to the top of the document first.
              I would try

              Selection.HomeKey  Unit:=wdStory
              Selection.GoTo What:=wdGoToHeading, Which:=wdGoToAbsolute, _
                  Count:=Me.lbAppxHeadings.ListIndex + 1

              You would have to test the first paragraph to make sure the selection point is not already sitting in the first heading. If it is then don’t include the +1

            • #543106

              That did the trick. Thanks!!

              I actually can get out of testing if I am in the heading by going to the very end of the document

              Selection.EndKey Unit:=wdStory

              and then running this code. Since the last item in the document is not likely to be a heading (unlike the first item), this code should work just fine.

              Thanks again for the help!!
              Troy

    Viewing 0 reply threads
    Reply To: Adding all headings to a list box (VBA for 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: