combo box list data

  • Thread starter OTWarrior via OfficeKB.com
  • Start date
O

OTWarrior via OfficeKB.com

I was using a form field drop down box, but I have reached the 25 limit.
Since word has no way around this, I having to use a combobox.

However, under the properties I am unable to see what generates the list in
the combobox.

Also, how do I generate the list in a macro?

NB: I am not using a userform as the microsoft support pages keeps saying, as
this is a document for people who are not on my network to use, not a program
for my own use. Having an mdb file with the values is not an option.

I have searched this site for any answers, and yet to find what I need, so
any input would be appreciated.
 
J

Jay Freedman

First consideration: A combobox or anything else from the Controls Toolbox
may behave badly. Read the "Appropriateness for Task" section of Cindy
Meister's article at
http://msdn2.microsoft.com/en-us/library/aa140269(office.10).aspx to see if
you can tolerate the shortcomings. IMHO you'd be much better served with a
userform (more about that in a bit).

Although Cindy's article, like whatever you read about userforms, shows how
to populate the combobox list from a database, that isn't necessary for
either a combobox or a userform. You can have the list data literally in the
macro itself, or stored as document variables or custom properties in the
document or its template, or in a separate Word document or Excel
spreadsheet or even a plain text file -- and I probably left out a few other
ways.

Here's a simple example: If this macro is in the ThisDocument module of the
document or of the template the document is based on, running it will put
the three strings into the combobox:

Sub demo()
Dim data As Variant
data = Array("one", "two", "three")
ComboBox1.List = data
ComboBox1.ListIndex = 0
End Sub

Instead of the Array statement, you can use various VBA algorithms to read
the data from wherever you chose to store it.

One thing to know: When you save and/or close the document containing the
combobox, it does _not_ store either the list or the index of the selected
item. Your code must do that. Typically, the code to populate the list is in
an AutoOpen or Document_Open macro so it runs each time the document is
reopened. The combobox's Change event handler has to store the list index of
the selected item, probably in a document variable, and the list-populating
macro needs to read that list index (if it exists) and set the .ListIndex
property to that value, so it appears to the user as if their previous
selection is still selected.

Regarding a userform: As I said before, a database is _not_ a requirement;
the data can be stored directly in the document and read by the macro that
displays the userform. Compared to the Control Toolbox's combobox, the
userform has the advantages that its display is stable, it uses much less
disk space and memory, and you don't have a big down-arrow button that also
prints with the document.

Typically you have a text form field whose entry macro displays the
userform, and the userform writes the selected value back into the form
field. That value will be saved with the document, and the macro can use it
to re-select in the list of the userform's control. (You might want to use a
listbox instead of a combobox there.) An alternative is for the userform to
write its result to a document variable, and have a DocVariable field in the
document to display its value.

Both Control Toolbox controls and userforms have a problem with the Word
security mechanism: If the security level is set to High or Very High, the
macros won't run. Also, if there are any controls in the document, then the
document opens in Design Mode and displays the toolbox, which is quite
confusing to most users.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top