How to Create a UserForm List Box in Outlookby Allen Bethea
Microsoft Outlook's Visual Basic for Applications macro programming language enables you to add a form to an email message that can collect data from its recipient. For example, you can insert a VBA UserForm and list box into your message that displays multiple items or options the recipient can choose from. Before you can add this feature to your messages, however, you need to add the Developer tab and its tools to the ribbon and change Outlook's security settings to allow VBA macros to run.
Click the "File" tab, "Options" and then "Customize Ribbon."
Check the box labeled "Developer" under Main Tabs and then click "OK." Outlook will automatically return to the Home panel.
Click the “File" tab and then "Options," "Trust Center" and "Trust Center Settings.”
Select "Macro Settings," "Enable all macros" and then click "OK" to allow the list box macro run during testing. Once you are done with your UserForm, however, you can change your settings to "Notifications for all macros" for more security.
Restart Outlook to activate the macro security settings change.
Click the "Developer" tab, "Macros" and then the "Macros" option.
Type a name for your macro and then click "Create."
Click the "Insert UserForm" button to display the form toolbox and create the UserForm that will contain your listbox.
Click the "Listbox” control in the Toolbox, hold down the mouse button, drag the control to the UserForm and then release it.
Click the "CommandButton” control in the Toolbox, hold down the mouse button, drag it to the UserForm and then release it.
Right-click the “CommandButton” in the UserForm and then click "View Code" to display its VBA macro code.
Add items to the list box using the AddItem function. For example, if you want the list box to contain the three primary colors, your code should resemble the following: Private Sub CommandButton1_Click() ListBox1.AddItem "Red" ListBox1.AddItem "Green" ListBox1.AddItem "Blue" End Sub ListBox1 is the name Outlook automatically gives the list box you dragged to the UserForm. The statement ListBox1.AddItem "Red" makes Red the first item in the list box.
Click "Save," press "F5" and then click the CommandButton to run the macro. When you run the macro and click the "CommandButton" the colors Red, Green and Blue will appear in the list box.
- Information in this article applies to Outlook 2013. It may vary slightly or significantly with other versions of Outlook.