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 FAQ: http://word.mvps.org
>I am attempting to design a customized word 2000 document that does several
>things:
[quoted text clipped - 36 lines]
>
>Thanks
Robbmac - 10 Mar 2005 03:53 GMT
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
[quoted text clipped - 88 lines]
> >
> >Thanks
Jay Freedman - 11 Mar 2005 04:01 GMT
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 FAQ: http://word.mvps.org
>Hi Jay, Thanks for your response.
>
[quoted text clipped - 124 lines]
>> >
>> >Thanks
MEME - 17 May 2008 20:43 GMT
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

Signature
meme
> No, you don't want to put that code in the ComboBox1_Change routine.
> That procedure gets called automatically by VBA whenever the user
[quoted text clipped - 172 lines]
> >> >
> >> >Thanks
Doug Robbins - Word MVP - 17 May 2008 22:15 GMT
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

Signature
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
> Hi Jay,
>
[quoted text clipped - 212 lines]
>> >> >
>> >> >Thanks