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

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

    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

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

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

    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
    <pre>myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)</pre>

    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.
    <pre>Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, count:=1, Name:=""</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

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

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

    Thanks for your reply. I found the statement:

    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)

    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

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

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

    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)
    <pre>Dim myHeadings as Variant
    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)
    Me.ListBox1.List = myHeadings</pre>

    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

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

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

    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

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

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

    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:

    <pre>Sub ListHeadings()
    Dim myHeadings As Variant, i As Integer
    myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)
    For i = 1 To UBound(myHeadings)
    Debug.Print myHeadings(i) 'change this to assigning it into the ListBox
    Next
    End Sub</pre>


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

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

    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(wdRefTypeHea ding)
    For i = 1 To UBound(DocumentHeadings)
    lbAppxHeadings.AddItem DocumentHeadings(i)
    Next i
    End Sub

    What am I missing?

    Thanks again!!
    Troy

  8. #8
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

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

    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?
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts

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

    Pardon my stepping in here... <img src=/S/hello.gif border=0 alt=hello width=25 height=29>

    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, <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15> Microsoft decreed that all events that belong to a userform -- any userform, regardless of its name -- are called Userform_XXXX(), not ActualUserformName_XXXX().

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

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

    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

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

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

    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
    <pre>Selection.HomeKey Unit:=wdStory
    Selection.GoTo What:=wdGoToHeading, Which:=wdGoToAbsolute, _
    Count:=Me.lbAppxHeadings.ListIndex + 1</pre>


    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
    Andrew Lockton, Chrysalis Design, Melbourne Australia

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

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

    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

Posting Permissions

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