Reference Multiple Comboboxes

A

Arkwright

Hi Folks,
Many thanks in advance.
I have a Word 2000 document (could be a 2003 doc, if that would help
with several comboboxes on it; one on each line of a table. The lis
items in each combobox are exactly the same (and in the same order)
lets say; Apples, Oranges, Pears, Bananas, for example.
If the user selects, Bananas, then a message box opens to say that the
are, say, Fair Trade from the Carribean".
My code works for the first Combobox, but I don't know how to make i
work for every single combobox in the document, without cutting
pasting it and then updating the combobox number. My code so far is:
Private Sub combobox2_Change()

If ComboBox2.Value = "Bananas" Then
MsgBox "These are Fair Trade from the Carribean."

End If
End Sub
I have played around with InlineShapes and managed to get the messag
to pop-up for several boxes, and guess that I need to change the su
name to something else rather than refering to combobox2, but don't kno
what or how?
Your help would gratefully be appreciated.
Cheer
 
J

Jay Freedman

Arkwright said:
Hi Folks,
Many thanks in advance.
I have a Word 2000 document (could be a 2003 doc, if that would help)
with several comboboxes on it; one on each line of a table. The list
items in each combobox are exactly the same (and in the same order),
lets say; Apples, Oranges, Pears, Bananas, for example.
If the user selects, Bananas, then a message box opens to say that
they are, say, Fair Trade from the Carribean".
My code works for the first Combobox, but I don't know how to make it
work for every single combobox in the document, without cutting &
pasting it and then updating the combobox number. My code so far is:
Private Sub combobox2_Change()

If ComboBox2.Value = "Bananas" Then
MsgBox "These are Fair Trade from the Carribean."

End If
End Sub
I have played around with InlineShapes and managed to get the message
to pop-up for several boxes, and guess that I need to change the sub
name to something else rather than refering to combobox2, but don't
know what or how?
Your help would gratefully be appreciated.
Cheers

You need to keep the _Change() procedure for each combobox, because that's
what automatically runs whenever the user changes the content of the
combobox -- you can't force Word to run some other event procedure instead.
The proper way is to create a subroutine that does the work, and call that
subroutine from each of the _Change() procedures. You can pass the identity
of the currently selected combobox as an argument to the subroutine. Here's
a sample:

Private Sub ComboBox1_Change()
DisplayText ComboBox1
End Sub

Private Sub ComboBox2_Change()
DisplayText ComboBox2
End Sub

Private Sub ComboBox3_Change()
DisplayText ComboBox3
End Sub

Private Sub DisplayText(whichBox As ComboBox)
Select Case whichBox.Value
Case "Apples"
MsgBox "These are from Washington State."
Case "Oranges"
MsgBox "These are Florida Oranges."
Case "Pears"
MsgBox "We don't know where we got these."
Case "Bananas"
MsgBox "These are Fair Trade from the Carribean."
Case Else
' do nothing
End Select
End Sub

See http://www.word.mvps.org/FAQs/MacrosVBA/ProcArguments.htm for more
explanation.

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

Arkwright

Jay said:
You need to keep the _Change() procedure for each combobox, becaus
that's
what automatically runs whenever the user changes the content of the
combobox -- you can't force Word to run some other event procedur
instead.
The proper way is to create a subroutine that does the work, and cal
that
subroutine from each of the _Change() procedures. You can pass th
identity
of the currently selected combobox as an argument to the subroutine
Here's
a sample:

Private Sub ComboBox1_Change()
DisplayText ComboBox1
End Sub

Private Sub ComboBox2_Change()
DisplayText ComboBox2
End Sub

Private Sub ComboBox3_Change()
DisplayText ComboBox3
End Sub

Private Sub DisplayText(whichBox As ComboBox)
Select Case whichBox.Value
Case "Apples"
MsgBox "These are from Washington State."
Case "Oranges"
MsgBox "These are Florida Oranges."
Case "Pears"
MsgBox "We don't know where we got these."
Case "Bananas"
MsgBox "These are Fair Trade from the Carribean."
Case Else
' do nothing
End Select
End Sub

See 'How to cut out repetition and write much less code, by usin
subroutines and functions that take arguments
(http://www.word.mvps.org/FAQs/MacrosVBA/ProcArguments.htm) for more
explanation.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: 'Home' (http://word.mvps.org)
Email cannot be acknowledged; please post all follow-ups to th
newsgroup so
all may benefit.

Jay,
Thank you for your swift reply. I feared as much, however, th
subroutine makes perfect sense, and will save time when they change th
wording - yes I like that.
Thanks for the education - only 199 boxes to go!
Have a great day
Cheers
Mar
 

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