Results 1 to 11 of 11
  1. #1
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    word 2003 tables (2003)

    I have a table in my document with 2 columns . I have a vba form that has a listbox on it I need to populate the list box with the text from each cell in column 2 of the table How?

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

    Re: word 2003 tables (2003)

    Welcome to Woody's Lounge!

    You can use code like this to get the text from the cells in column 2 in the first table in the document:

    Private Sub UserForm_Initialize()
    Dim c As Cell
    Dim strText As String
    For Each c In ActiveDocument.Tables(1).Columns(2).Cells
    strText = c.Range.Text
    Me.ListBox1.AddItem Left(strText, Len(strText) - 2)
    Next c
    End Sub

    The expression Left(strText, Len(strText) - 2) removes the last two characters from the cell text (a cell always ends in a paragraph mark and an end-of-cell marker).

    If you want to use another table, change Tables(1) as needed.
    If you want to use the currently selected table, or the table containing the current insertion point, change ActiveDocument to Selection.

  3. #3
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word 2003 tables (2003)

    Thanks will give that one a go but I have a feeling I will have others

  4. #4
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word 2003 tables (2003)

    Ok got that working Now How can I ensure that the items on the list are unique you see I need only one of each

  5. #5
    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: word 2003 tables (2003)

    Are the items in the table sorted so that all the matching items are together? In that case, you could check the value of the next item before adding it by comparing it against the last item in the listbox. If the items in the table are in no particular order, it is a bigger challenge.

  6. #6
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word 2003 tables (2003)

    The Items are not sorted at all but they will be one of 29 different items that are very consistent and will be in the following format Damp amd Mould (1) , Excess Cold (2), Excess Heat (3) The text will always be exactly the same for each item and they are taken from a previous form from a combobox that listed them by the number in brackets at the end. This means the text will always be the same for each of the 29 items that could be in the list John.

  7. #7
    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: word 2003 tables (2003)

    Considering the gymnastics, it might be faster to hardcode these 29 list items into your VBA. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

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

    Re: word 2003 tables (2003)

    Try this version:

    Private Sub UserForm_Initialize()
    Dim c As Cell
    Dim strText As String
    Dim col As New Collection
    Dim itm As Variant
    On Error Resume Next
    For Each c In ActiveDocument.Tables(1).Columns(2).Cells
    strText = c.Range.Text
    strText = Left(strText, Len(strText) - 2)
    col.Add Item:=strText, Key:=strText
    Next c
    For Each itm In col
    Me.ListBox1.AddItem itm
    Next itm
    End Sub

  9. #9
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word 2003 tables (2003)

    Ok folks I have now got them items on the list but try as I might I am unable to delete the duplicates I thought it would be fairly simple by Taking the first item in the list, comparing it to the other items in turn as a text string and then if the two strings are equal remove the second item. Then move on to the next item in the list and compare that etc etc. It seems though that my code for a loop within a loop fails because of variants. Is it not fairly simple to count the max number of items and cycle through them and then within that loop check the two strings delete the second and do this till the end of the list. The second cycle has to be offset by one more each time I think

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

    Re: word 2003 tables (2003)

    The code in my previous reply (<post:=736,746>post 736,746</post:>) removes duplicates.

  11. #11
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word 2003 tables (2003)

    Apologies Has I read that code all wrong, you are correct it does just that Great work many thanks

    John

Posting Permissions

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