|
|
|
date: Thu, 14 Aug 2008 07:32:00 -0700,
group: microsoft.public.word.vba.general
back
Re: Popuate List in ComboBox
Hi Matt,
A Userform is not required (although I tend to prefer them).
When you click the View Code button on the Control Toolbox while the combo
box is selected, what you get is misleading. You're being shown the code
procedure that will run when a user selects one of the items in the combo
box's list or types something into its box -- that's why the word "Change"
is in the procedure's name.
What you need to do instead is to write a procedure that runs when the
document is opened. To get that started, while the VBA editor is open, click
the dropdown at the top of the code window where it says "ComboBox1" and
choose "Document". VBA guesses wrong again and gives you a procedure named
Document_New (that's what runs when you use the File > New command to create
a new document from a template). Now click the other dropdown, which now
says "New", and select "Open". At last you have the procedure Document_Open
that you need.
Between "Private Sub Document_Open()" and "End Sub", put the kind of
statements that Hussain described.
You can delete the "Private Sub ComboBox1_Change()" and "Private Sub
Document_New()" lines and the "End Sub" lines that match them; or you can
just leave them there with no code inside them. Having an empty procedure is
almost the same as not having the procedure at all (it takes Word a few
milliseconds to "do nothing").
As a bit of background: Word doesn't save the items in a combo box's list
when it saves a document. Your code needs to rebuild that list every time
the document opens. That's why you have to use the Document_Open procedure.
For a lot more detail about the controls in the Toolbox, read
http://msdn2.microsoft.com/en-us/library/aa140269(office.10).aspx.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
Matt wrote:
> Thanks. Does the code below go between "Private Sub ComboBox1_Change
> ()" and "End Sub"? That is what I did and yet I cannot get it to
> populate the drop-down list.
>
> I placed the ComboBox directly into a Word doc, not a UserForm. Is a
> UserForm required?
>
> Thanks again for your help!
>
> "Hussain" wrote:
>
>> Just type the control's name and a dot, the available property names
>> will be shown.
>>
>> ComboBox1.AddItem "Apple"
>> ComboBox1.AddItem "Orange"
>> ComboBox1.AddItem "Carrot"
date: Thu, 14 Aug 2008 13:25:29 -0400
author: Jay Freedman
Re: Popuate List in ComboBox
Awesome explanation, Jay! Thanks a million! Works perfectly!
"Jay Freedman" wrote:
> Hi Matt,
>
> A Userform is not required (although I tend to prefer them).
>
> When you click the View Code button on the Control Toolbox while the combo
> box is selected, what you get is misleading. You're being shown the code
> procedure that will run when a user selects one of the items in the combo
> box's list or types something into its box -- that's why the word "Change"
> is in the procedure's name.
>
> What you need to do instead is to write a procedure that runs when the
> document is opened. To get that started, while the VBA editor is open, click
> the dropdown at the top of the code window where it says "ComboBox1" and
> choose "Document". VBA guesses wrong again and gives you a procedure named
> Document_New (that's what runs when you use the File > New command to create
> a new document from a template). Now click the other dropdown, which now
> says "New", and select "Open". At last you have the procedure Document_Open
> that you need.
>
> Between "Private Sub Document_Open()" and "End Sub", put the kind of
> statements that Hussain described.
>
> You can delete the "Private Sub ComboBox1_Change()" and "Private Sub
> Document_New()" lines and the "End Sub" lines that match them; or you can
> just leave them there with no code inside them. Having an empty procedure is
> almost the same as not having the procedure at all (it takes Word a few
> milliseconds to "do nothing").
>
> As a bit of background: Word doesn't save the items in a combo box's list
> when it saves a document. Your code needs to rebuild that list every time
> the document opens. That's why you have to use the Document_Open procedure.
>
> For a lot more detail about the controls in the Toolbox, read
> http://msdn2.microsoft.com/en-us/library/aa140269(office.10).aspx.
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
> Matt wrote:
> > Thanks. Does the code below go between "Private Sub ComboBox1_Change
> > ()" and "End Sub"? That is what I did and yet I cannot get it to
> > populate the drop-down list.
> >
> > I placed the ComboBox directly into a Word doc, not a UserForm. Is a
> > UserForm required?
> >
> > Thanks again for your help!
> >
> > "Hussain" wrote:
> >
> >> Just type the control's name and a dot, the available property names
> >> will be shown.
> >>
> >> ComboBox1.AddItem "Apple"
> >> ComboBox1.AddItem "Orange"
> >> ComboBox1.AddItem "Carrot"
>
>
>
date: Thu, 14 Aug 2008 10:36:08 -0700
author: Matt
|
|