Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Thu, 14 Aug 2008 07:32:00 -0700,    group: microsoft.public.word.vba.general        back       


Popuate List in ComboBox   
Hello.  I am very new to VBA and would like to use a drop-down list for the 
user to select an entry from the predetermined list.  How to do I create the 
list for the drop-down?
date: Thu, 14 Aug 2008 07:32:00 -0700   author:   Matt

Re: Popuate List in ComboBox   
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 08:57:37 -0700 (PDT)   author:   Hussain

Re: Popuate List in ComboBox   
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 09:24:06 -0700   author:   Matt

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

Re: Popuate List in ComboBox   
Matt,

Most of the methods shown here also apply to comboboxes:
http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm

Matt wrote:
> Hello.  I am very new to VBA and would like to use a drop-down list
> for the user to select an entry from the predetermined list.  How to
> do I create the list for the drop-down?

-- 
Greg Maxey -  Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
date: Thu, 14 Aug 2008 18:47:42 -0400   author:   Greg Maxey RrOMEOgOLF

Re: Popuate List in ComboBox   
Thanks, Greg!  I really appreciate yours, as well as everyone else's, help 
with this!

"Greg Maxey" wrote:

> Matt,
> 
> Most of the methods shown here also apply to comboboxes:
> http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm
> 
> Matt wrote:
> > Hello.  I am very new to VBA and would like to use a drop-down list
> > for the user to select an entry from the predetermined list.  How to
> > do I create the list for the drop-down?
> 
> -- 
> Greg Maxey -  Word MVP
> 
> My web site http://gregmaxey.mvps.org
> Word MVP web site http://word.mvps.org 
> 
> 
>
date: Fri, 15 Aug 2008 04:43:08 -0700   author:   Matt

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us