#1
|
|||
|
|||
Listbox, Multi-Select
I want to add a list box to my document allowing users to select multiple events from a list. I used the Developer tab to add a listbox, but it appears as an empty box, and I do not know how to populate it. I did select Properties and changed the Multi-Select field to MultiSelectExtended.
How do I populate my listbox? Thank you. P.S. I am using Word 365. |
#2
|
||||
|
||||
In a Word document I don't know of a listbox that can display the full list and allow multiselect. I have uses multiselect listboxs in vba userforms though.
The best I can suggest for a Word questionnaire is to use either 1. a dropdown list content control inside a repeating section content control 2. a series of check box content controls If the document is intended to be used in soft or hard copy form then option 2 is the way I would do it.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
||||
|
||||
One possibility is to use a rich text content control and use its OnEnter event to call a userform - see attached example.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
@gmayor
I opened Example.dotm and that is the kind of multi-select list I would like to create, but I am not sure of exactly what steps to take. From the Developer tab, I can insert a rich text content control but am not sure what to do next. I opened Visual Basic and saw the code you used. Should I click Visual Basic > Insert > Userform next? How do I pull up and populate the OnEnter Event? Thanks for further clarification. |
#5
|
||||
|
||||
Open your template and the attachment. From the VBA editor drag the userform from the example to your template. Copy the code from the ThisDocument module to the ThisDocument module of your template. Add a rich text content control to your template and title it "Link 1". (You can title it whatever you like, but make the change in the code to match.)
The ThisDocument module contains the code you need to change to supply your list Code:
Option Explicit Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl) Dim oFrm As UserForm1 'The name of the userform Dim i As Integer Dim orng As Range Dim sText As String If ContentControl.Title = "List 1" Then 'the name of the listbox Set oFrm = New UserForm1 With oFrm With .ListBox1 'the name of the listbox 'the items you want to add .AddItem "pizza" .AddItem "hamburgers" .AddItem "hot dogs" .AddItem "garden salad" .AddItem "potato salad" .AddItem "potato chips" .MultiSelect = fmMultiSelectMulti End With .Show With .ListBox1 'the name of the listbox For i = 0 To .ListCount - 1 If .Selected(i) = True Then sText = sText & .List(i) & vbCr End If Next i If Not sText = "" Then sText = Left(sText, Len(sText) - 1) End With Set orng = ContentControl.Range orng.Text = sText orng.End = orng.End + 1 orng.Collapse 0 orng.Select End With End If Unload oFrm Set oFrm = Nothing End Sub Code:
Option Explicit Private Sub CommandButton1_Click() Hide End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = vbFormControlMenu Then Cancel = True lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
list box, multi-select |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Listbox to select field name from data | JamesWood | Word VBA | 5 | 05-25-2021 02:42 PM |
Multi-Select CC Dropdown Lists | gmaxey | Word VBA | 1 | 01-10-2020 11:53 PM |
Yet another Multi-Select Listbox in Userform question | Javir | Word VBA | 4 | 09-24-2019 01:01 AM |
How to use an ActiveX Control to insert a multi select listbox in Word | marksm33 | Word | 2 | 01-29-2014 05:21 PM |
Multi-select listbox help | gvibe@hotmail.com | Word VBA | 1 | 07-19-2013 10:54 AM |