Very dumb question about initializing combo box that uses array

C

chowgirl

Here's the code I'm using to populate some very simple comboboxes with
hard-coded items:

Private Sub ComboBox1_Change()
ComboBox1.List = Array("Functional", "Look and feel", "Usability",
"Performance", "Operational", "Maintenance", "Security", "Legal",
"Other")
End Sub


Private Sub ComboBox2_Change()
ComboBox2.List = Array("1", "2", "3", "4", "5")
End Sub

Private Sub ComboBox3_Change()
ComboBox3.List = Array("1", "2", "3", "4", "5")
End Sub


The trouble is, when I close and re-open the .dot or .doc, the
comboboxes are empty. Do I have to use something other than array, such
as AddItem (or whatever it is)?

Thanks.
 
G

Greg Maxey

Put it in a UserForm Initialize routine

Private Sub UserForm_Initialize()
Me.ComboBox1.List = Array("Functional", "Look and feel", "Usability", _
"Performance", "Operational", "Maintenance", "Security", "Legal", _
"Other")
End Sub
 
G

Greg Maxey

I don't know. Try this.

Create a new blank document. Add a userform with 1 combobox and 1 command
button.

Show the form using:
Sub Kickoff()
Dim myFrm As UserForm1
Set myFrm = New UserForm1
myFrm.Show
Selection.Range.InsertAfter myFrm.ComboBox1.Text
Unload myFrm
Set myFrm = Nothing
End Sub

Put the following code in the userform:
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Dim myArray As Variant
Dim i As Long
myArray = Array("Functional", "Look and feel", "Usability", _
"Performance", "Operational", "Maintenance", "Security", "Legal", _
"Other")
For i = 0 To UBound(myArray) Step 1
Me.ComboBox1.AddItem myArray(i)
Next
End Sub

Work fine here.
 
J

Jay Freedman

I think the problem is that chowgirl is using the combo box from the
Control Toolbox directly in the document, not the combo box on a
userform.

That means that instead of a Userform_Initialize routine in userform
code, she needs to click the View Code button on the Control Toolbox
and create Document_New and Document_Open routines in the ThisDocument
module. Then the same code you showed in Userform_Initialize needs to
be placed inside each of those two routines.

When the File > New command is used to make a new document based on
the template, the Document_New routine will populate the combo box;
when an existing document is opened, the Document_Open routine will do
it.

There's a trickier problem to solve: If someone creates a document and
makes a selection in the combo box, they generally expect that saving,
closing, and reopening the document will result in the same selection
still being there. That takes extra code: The FileSave and FileSaveAs
routines need to be written to save the selection to a document
variable or some other storage, and the Document_Open routine needs
code to read that storage and make the corresponding selection.

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

Greg Maxey

Jay,

So for the UserFrom method something like this might do?

Option Explicit
Private Sub CommandButton1_Click()
ActiveDocument.Variables("Store").Value = Me.ComboBox1.Value
Me.Hide
End Sub

Private Sub UserForm_Initialize()
Dim myArray As Variant
Dim i As Long
myArray = Array("Functional", "Look and feel", "Usability", _
"Performance", "Operational", "Maintenance", "Security", "Legal", _
"Other")
For i = 0 To UBound(myArray) Step 1
Me.ComboBox1.AddItem myArray(i)
Next
On Error GoTo Oops
Me.ComboBox1.Value = ActiveDocument.Variables("Store").Value
Exit Sub
Oops:
Me.ComboBox1.Value = "Functional"
Resume Next
End Sub
 
A

arhooley

Chowgirl checking in from home. Thanks, all.

I stumbled across this snippet meanwhile:

Private Sub Document_Open()
InitCombo
End Sub
Private Sub InitCombo()
Dim Entries As Variant
Entries = Array("one", "two", "three")
ComboBox1.List = Entries
ComboBox1.ListIndex = 0
End Sub


That works fine for a single combobox inserted straight into a Word
doc--my boss wants this in a .doc file now. I fiddled around with it
quite a bit since I need a total of three comboboxes for the doc. Sorry
to be such a slob--I studied poetry and music and now I'm earning my
living as a technical writer--but how would I code all three
comboboxes?

Thanks.
 
A

arhooley

Thanks, all

I also got some help from this snippet:

Private Sub Document_Open()
InitCombo
End Sub

Private Sub InitCombo()
Dim Entries As Variant
Entries = Array("one", "two", "three")
ComboBox1.List = Entries
ComboBox1.ListIndex = -1
End Sub
 
J

Jay Freedman

Hi Greg,

Yep, that does it. :)

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

Jay Freedman

Chowgirl checking in from home. Thanks, all.

I stumbled across this snippet meanwhile:

Private Sub Document_Open()
InitCombo
End Sub
Private Sub InitCombo()
Dim Entries As Variant
Entries = Array("one", "two", "three")
ComboBox1.List = Entries
ComboBox1.ListIndex = 0
End Sub


That works fine for a single combobox inserted straight into a Word
doc--my boss wants this in a .doc file now. I fiddled around with it
quite a bit since I need a total of three comboboxes for the doc. Sorry
to be such a slob--I studied poetry and music and now I'm earning my
living as a technical writer--but how would I code all three
comboboxes?

Thanks.

Just initialize each combo box in turn. It can get fancier, but at a
minimum:

Private Sub InitCombo()
Dim Entries As Variant
Entries = Array("one", "two", "three")
ComboBox1.List = Entries
ComboBox1.ListIndex = 0

Entries = Array("four", "five", "six", "seven")
ComboBox2.List = Entries
ComboBox2.ListIndex = 0

Entries = Array("eight", "nine")
ComboBox3.List = Entries
ComboBox3.ListIndex = 0
End Sub

This re-uses the variable Entries, giving it a different set of values
each time. That set of values is then assigned to the next combo box,
and that combo box's ListIndex is set to 0 (which makes the first item
appear in the top of the box).

Notice how the number of items doesn't have to be the same in each
box.

--
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