The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Add and Remove Items from a List Box

Office 2007

The following example adds and deletes the contents of a ListBox using the AddItem, RemoveItem, and SetFocus methods, and the ListIndex and ListCount properties.

Bb176481.vs_note(en-us,office.12).gif  Note
The SetFocus method is inherited from the Microsoft Forms 2.0 ListBox control.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains:

  • A ListBox named ListBox1.
  • Two CommandButton controls named CommandButton1 and CommandButton2.
Dim EntryCount 
Dim Listbox1

Sub Item_Open()
  Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").ListBox1
  Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton1
  Set CommandButton2 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton2
  EntryCount = 0
  CommandButton1.Caption = "Add Item"
  CommandButton2.Caption = "Remove Item"
End Sub

Sub CommandButton1_Click()
  EntryCount = EntryCount + 1
  ListBox1.AddItem (EntryCount & " - Selection")
End Sub

Sub CommandButton2_Click()

  'Ensure ListBox contains list items
  If ListBox1.ListCount >= 1 Then
      'If no selection, choose last list item.
      If ListBox1.ListIndex = -1 Then
          ListBox1.ListIndex = ListBox1.ListCount - 1
      End If
      ListBox1.RemoveItem (ListBox1.ListIndex)
  End If
End Sub

Community Additions