I created a drop down list in Power Point 2010, which works very well before saving the document. I used the following code in VBA:
Sub AddItemsToSelectedListBox()
Dim oShape As Shape
Set oShape = ActiveWindow.Selection.ShapeRange(1)
With oShape.OLEFormat.Object
' Add items to the list
.AddItem ("Good")
.AddItem ("Better")
.AddItem ("Best")
' You could work with other properties of the list or combo box here as well
End With
End Sub
f5 + close.
By going to the view modus, the drop down list works well. But if I save my power point document (in a .pptm format) and reopen the presentation, the lists don't drop down any more. If I enter the VBA, the code looks like this:
Sub AddItemsToSelectedListBox()
Dim oShape As Shape
Set oShape = ActiveWindow.Selection.ShapeRange(1)
With oShape.OLEFormat.Object
' Add items to the list
.AddItem ("Good")
.AddItem ("Better")
.AddItem ("Best")
' You could work with other properties of the list or combo box here as well
End With
End Sub
Private Sub ComboBox1_Change()
End Sub
The ComboBox_Change() Part is new. (Why?)
Does anyone know, how to generate a dropdown list that survives the saving-process?
Thank you very much!!
The _Change subroutine would be added when you doubleclick the combo box to get into the VBE.
If you load the combo box from a subroutine in a module, it doesn't seem to retain the values when you save and reopen.
If you load the combo box from an event in the box itself, it seems to work as you'd expect.
For example:
Private Sub ComboBox1_GotFocus()
Dim oShape As Shape
Set oShape = ActivePresentation.Slides(1).Shapes("ComboBox1")
With oShape.OLEFormat.Object
.Clear
' Add items to the list
.AddItem ("Good")
.AddItem ("Better")
.AddItem ("Best")
' You could work with other properties of the list or combo box here as well
End With
End Sub
Related
How can I load in the answer (item) selected previously for a combobox in a userform.
For instance lets say I had combobox1 that has items below loaded upon initialization:
With ComboBox1
.AddItem "Yes"
.AddItem "No"
End With
How could I load the selected item back in upon reinitialization of the form, so if a user selected his answer, saved, exited the userform and word doc, then reopened the userform. Basically I want the user to see their selection previously chosen to be loaded into combobox1
I have this working for textboxes attached to docvariables, but the comboboxes are attached to bookmarks.
The textboxes reinitializing code is shown below:
Private Sub UserForm_Initialize()
Dim oVars As Object
Set oVars = ActiveDocument.Variables
On Error Resume Next
TextBox1.Value = oVars("bmtitle").Value
Comboboxes are setup like:
Private Sub CommandButton1_Click()
Dim ComboBox1 As Range
Set ComboBox1 = ActiveDocument.Bookmarks("bmextension").Range
ComboBox1.Text = Me.ComboBox1.Value
If Me.ComboBox1.Value = "Yes" Then
ComboBox1.Text = "For additional information see CF4"
End If
If Me.ComboBox1.Value = "No" Then
ComboBox1.Text = ""
End If
I'm looking for a way to add items to a combobox list without adding the item to the code directly.
UserForm Image
Private Sub UserForm_Initialize()
With ComboBox2
.AddItem ".020"
.AddItem ".030"
.AddItem ".032"
.AddItem ".040"
End With
With ComboBox3
.AddItem "THK"
.AddItem "DIA"
.AddItem "TUBE"
.AddItem "FORGING"
End With
End Sub
I'd like the user to be able to add an Item into the dropdown list without going into the code. Is there a way to add an .AddItem to the combobox?
Unlike ListBoxes, ComboBoxes allow a user to type a new entry directly into the comboBox field. That's the simplest solution, no extra code required!
If, for some reason, you want to make this more complicated, you can add a text field and a command button. After the user types a new entry in the text field, they click on the button which runs this macro:
Private Sub CommandButton1_Click()
ComboBox3.AddItem TextBox1.value
End Sub
All,
I have created a Userform (Userform 3) which I wish to have a basic drop down list of 4 Items - Apples / Oranges / Pears / bananas. (Hard coded in the background VBA) and if none of the selections are selected by the users to exit sub.
Please can someone advise how I would do this?
Inserted picture for reference?
To populate the Fruits_CB combo-box on UserForm_Initialize event use the code below:
Private Sub UserForm_Initialize()
' populate "Fruits_CB" Combo-Box with fruits
With Me.Fruits_CB
.Clear ' clear previous items (not to have "doubles")
.AddItem "Apples"
.AddItem "Oranges"
.AddItem "Pears"
.AddItem "Bananas"
End With
End Sub
If the user is not selecting any item from the list, and then clicks the button, use the Sub below:
Private Sub CommandButton1_Click()
If Me.Fruits_CB.ListIndex = -1 Then
Exit Sub
Else
' Do something
End If
End Sub
I have a userform with a lot of comboboxes. I am trying to use .AddItem when the userform is intialized, with no success.
Here is what I have tried so far:
Sub Userform_Initialize()
Dim cCont As MSForms.ComboBox
For Each cCont In Me.Controls
cCont.AddItem "Item Added"
Next cCont
End Sub
I usually get a runtime error 13 "type mismatch" come up. Not sure if that has anything to do with the button I have on the userform as well.
How do I loop through a userform and use .AddItem to each combobox? Once I get the code on how to do this, I'll wrap it in an If statement to only add it to certain comboboxes with certain words in the name, if that helps for context.
Thanks in advance,
-Anthony
Anthony,
You are trying to call the AddItem method on each control on the form. But not all controls provide the AddItem method. You need to check out whether a particular control is combobox or not. And only if the control is combobox you may call the AddItem method.
How do I loop through a userform and use .AddItem to each combobox?
It is not clear where you need to loop through. Could you be more specific?
The best way to add controls to a userform that I have found is to follow this procedure.
Dim NewComboBox as Control
Set NewComboBox = Me.Controls.Add("Forms.ComboBox.1")
With NewComboBox
'Inside of this part you can put .name or any other property in the
'activex controls properties part, .left .top .width and .height determine the size of the box
.Name = Whatever Name You Desire
End With
Put this in your userform_initialize() event and you should be good to go.
You code works fine for me adding two combo boxes to a userform. Only thing I can think of is the correct references are not enabled for some reason.
One question, it is possible to jump somehow to a certain index in a specif listbox as in the image below?
I already tried the following code
Listbox.ListIndex = index
But it drives me to the error You've used the ListIndex property incorrectly
One property of my list that might be important to mention.
Row source type : Table/Query
Thank you in advance.
Try ListBox.Selected(index) = True. If it is a multiselect listbox, you also need to loop through the other elements and unselect them in the same way.
Create a standard module with the code
Sub Main()
UserForm1.Show
Unload UserForm1
End Sub
Insert a userform and visually do something like
Go into the userform code and add
Private Sub CommandButton1_Click()
Dim v As Long
For v = 0 To ListBox1.ListCount - 1
If TextBox1 = ListBox1.List(v) Then
ListBox1.Selected(v) = True
End If
Next v
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem ("text1")
.AddItem ("text2")
.AddItem ("text3")
End With
End Sub
Run Main Macro
Type in the box : text2
The text2 will be selected in the list