Controls

G

Greg Maxey

Hi,

If I open a new document, insert a Combobox with the Controls toolbar, name the combobox "myCB" and then enter the code:

Sub Test()
myCB.AddItem "A"
End Sub

in the "ThisDocument" area of Project(Document1)

As I enter that code and type myCB., the dropdown list appears so I can select "AddItem," and the code runs with no error.

Now if I add a module "Module1" and enter that same code, the dropdown doesn't appear as I type myCB. and when I run the code, I get the error:

Compile error: Variable not defined on the myCB statement.

Now if I enter this code in the module it works:

Sub Test()
Dim myCB As ComboBox
Set myCB = ActiveDocument.myCB
With myCB
.Clear
.AddItem "A"
End With
End Sub

However as I type Set myCB = ActiveDocument. (the dropdown list does not appear with the option to select myCB

So I guess my question is, Can someone explain what is going on under the hood in Word here and what is the proper way to declare and then use a control in a Word form.

Thanks
 
T

Tony Jollans

Hi Greg,

When you have inserted the combobox, it belongs to the document. Editing
code in 'ThisDocument' you have a default qualification - you are in the
document, as it were, and so is the combo box and so you can see each other.

When you are in another module it's like going into another room in the
house and you can't see the combobox any more; VBA won't play hide and seek
and so you must tell it where to find the combobox.

Lastly, when you use ActiveDocumnet that is just a generic document
identifier and only default elements can be seen. One of my attributes, if
you like, is that I wear glasses but in a general description of a person,
glasses are not a feature. Only when looking at the specific document
("ThisDocument", for example) will VBA prompt with its specific properties.
If you have a description explicitly of me, it will include my specific
glasses in other words there is a Tony.Glasses option, but not a
Person.Glasses.

Does that muddy the waters enough?

--
Enjoy,
Tony

Hi,

If I open a new document, insert a Combobox with the Controls toolbar, name
the combobox "myCB" and then enter the code:

Sub Test()
myCB.AddItem "A"
End Sub

in the "ThisDocument" area of Project(Document1)

As I enter that code and type myCB., the dropdown list appears so I can
select "AddItem," and the code runs with no error.

Now if I add a module "Module1" and enter that same code, the dropdown
doesn't appear as I type myCB. and when I run the code, I get the error:

Compile error: Variable not defined on the myCB statement.

Now if I enter this code in the module it works:

Sub Test()
Dim myCB As ComboBox
Set myCB = ActiveDocument.myCB
With myCB
.Clear
.AddItem "A"
End With
End Sub

However as I type Set myCB = ActiveDocument. (the dropdown list does not
appear with the option to select myCB

So I guess my question is, Can someone explain what is going on under the
hood in Word here and what is the proper way to declare and then use a
control in a Word form.

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