Creating, Populating and Linking combo boxes Word 2K

R

Robbmac

I am attempting to design a customized word 2000 document that does several
things:
1. Upon opening, it populates several demographic fields (like patient
name, address etc.) from a health care Practice Management Software database.
The PMS company has provided a "generic document" with a bunch of fields
embedded in the document that are linked by VBA code to their database. The
idea is that you can create a custom form (say a prescription blank) by
modifying the generic document, deleting the fields you don't want on the
form and adding text, graphics and controls as desired. The new customized
form is then initialized in the PMS and the document is opened by the PMS
referencing a patient. Upon opening an auto open macro initiates VBA code
which populates the fields with data on the patient in question. All of this
works fine to this point.

2. I also want to place some VBA controls (specifically, drop down lists
and combo boxes) in the document. I can place a combo box on the document
from the Controls Toolbar but I can't figure out how to populate the list
with choices.

3. I want to create "primary" combo boxes with lists of categories and I
want
to link those controls to "secondary" controls such that the choice made in
the primary combo box determines the choices that populate the secondary
combo box. I understand that populating the secondary combo boxes would
need to be done with VBA code.

I am somewhat familiar with using VBA in Access and I think I could probably
do steps 2 and 3 above in Access. However, the controls don't seem to work
the same in Word.

Finally, I've been trying to find a basic/introductory VBA reference for
Word 2000
without any success. There are plenty for Access and Excel but I haven't
found any that cover Word VBA applications.

Can anyone point me in the right direction on my project, and does anyone
have any recommendations for published or online resources for using VBA with
Word?

Thanks
 
J

Jay Freedman

For the "primary" combo boxes, you populate the list with VBA code you
call from the Document_New and Document_Open procedures in the
ThisDocument module of the template. When you insert the combo box in
the document, click the View Code button on the toolbar. This creates
an empty _Change procedure for the combo box itself. In the VBA
editor, use the two dropdowns just above the code window to create the
two Document procedures.

You have a choice of ways to insert the items in the list. One is to
call the combo box's .AddItem method once for each item:

With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
' etc.
.ListIndex = 0
End With

Another way is to construct an array in a Variant variable, and then
assign that array to the combo box's .List property:

Dim Items As Variant
Items = Array("Category 1", "Category 2")
With ComboBox1
.Clear
.List = Items
.ListIndex = 0
End With

Setting the .ListIndex to 0 instead of the default -1 ensures that the
first item appears in the box instead a blank.

When it comes to setting the "secondary" lists, you run code in the
ComboBox1_Change procedure. You can either use a similar scheme to
those above, but choosing the items on the basis of the value of
ComboBox1.Text; or you can extract the items from a database. For
that, see the example at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnword2k2/html/odc_activeX.asp
which also has a lot of other good information about using ActiveX
controls in a Word document.

For general Word VBA information, there isn't much in print. Online,
look through the articles at http://word.mvps.org/FAQs/MacrosVBA.htm
and http://word.mvps.org/FAQs/OfficeInterdev.htm. And we're here 24-7.
:)
 
R

Robbmac

Hi Jay, Thanks for your response.

I think I almost understand how to populate the "primary" boxes but can't
quite make it happen.
When you say:
For the "primary" combo boxes, you populate the list with VBA code you
call from the Document_New and Document_Open procedures

Is this what you mean:

Private Sub ComboBox1_Change()
With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
.ListIndex = 0
End With
End Sub

Private Sub Document_New()
ComboBox1_Change()
End Sub

Private Sub Document_Open()
ComboBox1_Change()
End Sub

Obviously it's not what you meant because it gives me a compile error.
Also what is the difference between "Private Sub Document_Open()" and
"Private Sub Document_New()" and does the ComboBox1_Change()
procedure need to be called from both?

I'm sorry to hear that there's not much published on Word/VBA, but I'm
glad for your help and for the resources you've pointed out.
 
J

Jay Freedman

No, you don't want to put that code in the ComboBox1_Change routine.
That procedure gets called automatically by VBA whenever the user
chooses a different item in the combo box (it's called an "event
procedure" because it gets called when an event happens -- in this
case, a change of selection in the box).

The Document_New and Document_Open procedures are also event
procedures. The former is called whenever the user uses the File > New
dialog to create a document based on your template, and the latter is
called whenever an existing document based on your template is
reopened.

Make another procedure, *not* an event procedure, and call that from
both Document_New and Document_Open. The name of the procedure can be
anything you want *except* something that's already taken for an event
procedure or a built-in command. So let's say you call it
MyLoadList...

Private Sub MyLoadList()
With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
.ListIndex = 0
End With
End Sub

Private Sub Document_New()
MyLoadList
End Sub

Private Sub Document_Open()
MyLoadList
End Sub

[The reason you got a compile error is probably the empty parentheses
you used in the calls to ComboBox1_Change().]

What *does* belong in ComboBox1_Change() is the code to load
ComboBox2's list with the entries appropriate to the current value in
ComboBox1. I suspect you'll need help with that, too, but it's too
late now for me to think straight about that... G'night.
 
M

MEME

Hi Jay,

I have been reading information. I have the exact same question and I did
what you have recomented here. It is not working. It is empty. Can you try to
help! What could be my problem:

I did the following.

A template with a combo box (combobox1) and I added the routines posted
document_new() document_open()...

thanks
MEME
--
meme


Jay Freedman said:
No, you don't want to put that code in the ComboBox1_Change routine.
That procedure gets called automatically by VBA whenever the user
chooses a different item in the combo box (it's called an "event
procedure" because it gets called when an event happens -- in this
case, a change of selection in the box).

The Document_New and Document_Open procedures are also event
procedures. The former is called whenever the user uses the File > New
dialog to create a document based on your template, and the latter is
called whenever an existing document based on your template is
reopened.

Make another procedure, *not* an event procedure, and call that from
both Document_New and Document_Open. The name of the procedure can be
anything you want *except* something that's already taken for an event
procedure or a built-in command. So let's say you call it
MyLoadList...

Private Sub MyLoadList()
With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
.ListIndex = 0
End With
End Sub

Private Sub Document_New()
MyLoadList
End Sub

Private Sub Document_Open()
MyLoadList
End Sub

[The reason you got a compile error is probably the empty parentheses
you used in the calls to ComboBox1_Change().]

What *does* belong in ComboBox1_Change() is the code to load
ComboBox2's list with the entries appropriate to the current value in
ComboBox1. I suspect you'll need help with that, too, but it's too
late now for me to think straight about that... G'night.

--
Regards,
Jay Freedman
Microsoft Word MVP

Hi Jay, Thanks for your response.

I think I almost understand how to populate the "primary" boxes but can't
quite make it happen.
When you say:


Is this what you mean:

Private Sub ComboBox1_Change()
With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
.ListIndex = 0
End With
End Sub

Private Sub Document_New()
ComboBox1_Change()
End Sub

Private Sub Document_Open()
ComboBox1_Change()
End Sub

Obviously it's not what you meant because it gives me a compile error.
Also what is the difference between "Private Sub Document_Open()" and
"Private Sub Document_New()" and does the ComboBox1_Change()
procedure need to be called from both?

I'm sorry to hear that there's not much published on Word/VBA, but I'm
glad for your help and for the resources you've pointed out.
 
D

Doug Robbins - Word MVP

With ActiveDocument.InlineShapes(1).OLEFormat
.Activate
Set myObj = .Object
End With
With myObj
For i = 1 To 3
.AddItem "Item" & i
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

MEME said:
Hi Jay,

I have been reading information. I have the exact same question and I did
what you have recomented here. It is not working. It is empty. Can you try
to
help! What could be my problem:

I did the following.

A template with a combo box (combobox1) and I added the routines posted
document_new() document_open()...

thanks
MEME
--
meme


Jay Freedman said:
No, you don't want to put that code in the ComboBox1_Change routine.
That procedure gets called automatically by VBA whenever the user
chooses a different item in the combo box (it's called an "event
procedure" because it gets called when an event happens -- in this
case, a change of selection in the box).

The Document_New and Document_Open procedures are also event
procedures. The former is called whenever the user uses the File > New
dialog to create a document based on your template, and the latter is
called whenever an existing document based on your template is
reopened.

Make another procedure, *not* an event procedure, and call that from
both Document_New and Document_Open. The name of the procedure can be
anything you want *except* something that's already taken for an event
procedure or a built-in command. So let's say you call it
MyLoadList...

Private Sub MyLoadList()
With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
.ListIndex = 0
End With
End Sub

Private Sub Document_New()
MyLoadList
End Sub

Private Sub Document_Open()
MyLoadList
End Sub

[The reason you got a compile error is probably the empty parentheses
you used in the calls to ComboBox1_Change().]

What *does* belong in ComboBox1_Change() is the code to load
ComboBox2's list with the entries appropriate to the current value in
ComboBox1. I suspect you'll need help with that, too, but it's too
late now for me to think straight about that... G'night.

--
Regards,
Jay Freedman
Microsoft Word MVP

Hi Jay, Thanks for your response.

I think I almost understand how to populate the "primary" boxes but
can't
quite make it happen.
When you say:

For the "primary" combo boxes, you populate the list with VBA code you
call from the Document_New and Document_Open procedures

Is this what you mean:

Private Sub ComboBox1_Change()
With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
.ListIndex = 0
End With
End Sub

Private Sub Document_New()
ComboBox1_Change()
End Sub

Private Sub Document_Open()
ComboBox1_Change()
End Sub

Obviously it's not what you meant because it gives me a compile error.
Also what is the difference between "Private Sub Document_Open()" and
"Private Sub Document_New()" and does the ComboBox1_Change()
procedure need to be called from both?

I'm sorry to hear that there's not much published on Word/VBA, but I'm
glad for your help and for the resources you've pointed out.


:

For the "primary" combo boxes, you populate the list with VBA code you
call from the Document_New and Document_Open procedures in the
ThisDocument module of the template. When you insert the combo box in
the document, click the View Code button on the toolbar. This creates
an empty _Change procedure for the combo box itself. In the VBA
editor, use the two dropdowns just above the code window to create the
two Document procedures.

You have a choice of ways to insert the items in the list. One is to
call the combo box's .AddItem method once for each item:

With ComboBox1
.Clear
.AddItem "Category 1"
.AddItem "Category 2"
' etc.
.ListIndex = 0
End With

Another way is to construct an array in a Variant variable, and then
assign that array to the combo box's .List property:

Dim Items As Variant
Items = Array("Category 1", "Category 2")
With ComboBox1
.Clear
.List = Items
.ListIndex = 0
End With

Setting the .ListIndex to 0 instead of the default -1 ensures that the
first item appears in the box instead a blank.

When it comes to setting the "secondary" lists, you run code in the
ComboBox1_Change procedure. You can either use a similar scheme to
those above, but choosing the items on the basis of the value of
ComboBox1.Text; or you can extract the items from a database. For
that, see the example at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnword2k2/html/odc_activeX.asp
which also has a lot of other good information about using ActiveX
controls in a Word document.

For general Word VBA information, there isn't much in print. Online,
look through the articles at http://word.mvps.org/FAQs/MacrosVBA.htm
and http://word.mvps.org/FAQs/OfficeInterdev.htm. And we're here 24-7.
:)

--
Regards,
Jay Freedman
Microsoft Word MVP

On Wed, 9 Mar 2005 16:15:03 -0800, "Robbmac"

I am attempting to design a customized word 2000 document that does
several
things:
1. Upon opening, it populates several demographic fields (like
patient
name, address etc.) from a health care Practice Management Software
database.
The PMS company has provided a "generic document" with a bunch of
fields
embedded in the document that are linked by VBA code to their
database. The
idea is that you can create a custom form (say a prescription blank)
by
modifying the generic document, deleting the fields you don't want on
the
form and adding text, graphics and controls as desired. The new
customized
form is then initialized in the PMS and the document is opened by the
PMS
referencing a patient. Upon opening an auto open macro initiates VBA
code
which populates the fields with data on the patient in question. All
of this
works fine to this point.

2. I also want to place some VBA controls (specifically, drop down
lists
and combo boxes) in the document. I can place a combo box on the
document
from the Controls Toolbar but I can't figure out how to populate the
list
with choices.

3. I want to create "primary" combo boxes with lists of categories
and I
want
to link those controls to "secondary" controls such that the choice
made in
the primary combo box determines the choices that populate the
secondary
combo box. I understand that populating the secondary combo boxes
would
need to be done with VBA code.

I am somewhat familiar with using VBA in Access and I think I could
probably
do steps 2 and 3 above in Access. However, the controls don't seem
to work
the same in Word.

Finally, I've been trying to find a basic/introductory VBA reference
for
Word 2000
without any success. There are plenty for Access and Excel but I
haven't
found any that cover Word VBA applications.

Can anyone point me in the right direction on my project, and does
anyone
have any recommendations for published or online resources for using
VBA with
Word?

Thanks
 

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