Results 1 to 9 of 9
  1. #1
    New Lounger
    Join Date
    Jun 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Items in ComboBox (Word XP)

    Have a Combo in a UserForm, and would like to get the names in the Combo from ex. Excel to make it more flexible. Which is the best way to do this? I would also like to create a command like "add new member" and "delete member" to manipulate the list from Word.

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

    Re: Items in ComboBox (Word XP)

    To add or remove an item from a list box, use the AddItem and RemoveItem methods.

    To load a series of items into a list box, set the List property to an array.

    If you click on a list box in a user form (in design view), and press F1, you get help on the ListBox object, with an overview of its properties, methods and events; the help also contains numerous code examples.

    I recommend that you familiarize yourself with these; if you still want to use Excel after that, post back.

  3. #3
    New Lounger
    Join Date
    Jun 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Items in ComboBox (Word XP)

    Thanks again. What I meant about adding / deleting was that I would like to have the possibility to make changes in the list from the combobox. If i from the box choose one name, to be able to delete it from the list, och to write a new name, and from there add it to the list..I don't suppose that is possible from a listbox??

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

    Re: Items in ComboBox (Word XP)

    Yes, you can. For a combo box named cbxCombo, you could use code like this:

    Private Sub cmdAdd_Click()
    Me.cbxCombo.AddItem Me.cbxCombo.Text
    End Sub

    Private Sub cmdDelete_Click()
    If Me.cbxCombo.ListIndex > -1 Then
    Me.cbxCombo.RemoveItem Me.cbxCombo.ListIndex
    ' Optional: clear text box
    Me.cbxCombo.Text = ""
    End If
    End Sub

    See attached zipped document. The document itself is empty, switch to the Visual Basic Editor to inspect the userform. If you click in an empty part of the form and press F5, the form will be "run".
    Attached Files Attached Files

  5. #5
    New Lounger
    Join Date
    Jun 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Items in ComboBox (Word XP)

    Wow, you have saved me a lot of work. Thanks a lot!

  6. #6
    New Lounger
    Join Date
    Jun 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Items in ComboBox (Word XP)

    But let's say I use Combos in a lot of separate places, but they shall have the same content in all cases, wouldn't it be easier to get the content from some kind of database or list? So that if I add a name, it is added in the combo wherever I use it?

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

    Re: Items in ComboBox (Word XP)

    The list in a combo box in a userform in Word is not bound dynamically to a table, you would have to use code to change the list for all other list boxes / combo boxes too. You could store the items in a global dynamic array (declared in a standard module, not in the userform module), and use the command buttons to add and remove iitems from this array, but you would still have to use code to update all other list boxes / combo boxes on the same form.

    The same would hold even in a database application created in Access. Although there, you can set the row source of a list box / combo box to a database table, you still must requery if you add or remove a record in the table.

  8. #8
    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: Items in ComboBox (Word XP)

    Over the weekend I was working on a listbox project. For both a listbox and a combobox there is a ControlSource property for which the online help says "Identifies the data location used to set or store the Value property of a control. The ControlSource property accepts worksheet ranges from Microsoft Excel." However, I don't know whether the integration is tight or sloppy; I ended up "rolling my own."

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

    Re: Items in ComboBox (Word XP)

    Jefferson,

    The help item is misleading; you could easily think that you can enter an Excel range in the ControlSource property in any application, but that only works in Excel itself. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

Posting Permissions

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