Manipulating Listbox and Combobox controls on slides and forms
You can manipulate List and combo boxes on slides using the same properties as you can use to work with list and combo boxes on user forms, but when writing code, PowerPoint's intellisense feature doesn't help when you're working with such objects on a slide.
To work with them, you need to get a reference to the shape's OLEFormat.Object and manipulate it. The following example shows how:
Sub AddItemsToSelectedListBox()
Dim oShape As Shape
Set oShape = ActiveWindow.Selection.ShapeRange(1)
With oShape.OLEFormat.Object
' Add items to the list
.AddItem ("This")
.AddItem ("That")
.AddItem ("The Other")
' You could work with other properties of the list or combo box here as well
End With
End Sub
Here's another example. Run InitListBox once to add a number for each slide in the presentation to ComboBox1.
When you choose any number from the combobox during a slide presentation, Sub ComboBox1_Change will send you to that numbered slide in the presentation
Private Sub ComboBox1_Change()
On Error Resume Next
SlideShowWindows(1).View.GotoSlide (CLng(ComboBox1))
End Sub
Sub InitListBox()
Dim oShape As Shape
Dim X As Long
Set oShape = ActiveWindow.Selection.ShapeRange(1)
With oShape.OLEFormat.Object
' Delete any existing items
.Clear
' Add items to the list - one for each slide
For X = 1 To ActivePresentation.Slides.Count
.AddItem (CStr(X))
Next
' You could work with other properties of the list or combo box here as well
End With
End Sub
And another example that demonstrates how to load a combo box from an array. This one works with a combobox on a user form.
The form should contain a combobox named cboPicklist and a label named Label1.
Private Sub UserForm_Initialize()
' We'll load the combobox in the userform's Initialize event
Dim i As Long
Dim MyArray(6, 3)
'Set the combo for 3 data columns, set their widths
With Me.cboPicklist
.ColumnCount = 3
.Columnwidths = "12;36;36"
End With
'Load integer values into first column of MyArray
For i = 0 To 5
MyArray(i, 0) = i
Next i
'Load columns 2 and three of MyArray; pardon my French
MyArray(0, 1) = "Zero"
MyArray(1, 1) = "One"
MyArray(2, 1) = "Two"
MyArray(3, 1) = "Three"
MyArray(4, 1) = "Four"
MyArray(5, 1) = "Five"
MyArray(0, 2) = "Zero"
MyArray(1, 2) = "Un ou Une"
MyArray(2, 2) = "Deux"
MyArray(3, 2) = "Trois"
MyArray(4, 2) = "Quatre"
MyArray(5, 2) = "Cinq"
' Load the combo box by assigning the array to it
Me.cboPicklist.List() = MyArray
' set the selection to the first item on the list
' (list is zero-based)
Me.cboPicklist.ListIndex = 0
End Sub
Private Sub cboPicklist_Change()
' Whenever you pick something in the combobox, it will display here
Me.Label1.Caption = Me.cboPicklist.Text
End Sub
See How do I use VBA code in PowerPoint? to learn how to use this example code.