Working with controls in a word document

M

Martin Newman

In WOrdXP

I have a word form (not a user form, but a word doicument created using the
form facilities) in which I wish to manipulate the controls I have in a
form. If I use code like:

Dim ctlFrom As Control

Set ctlFrom = Me..lstbxSubjectSelected

it doesn't work, type mismatch as the right hand side is evaluated to the
value of the default property of the control rather than the control itself.

And as there is no COntrols collection for a WOrd document code like this
fails too:

Set ctlFrom = Me.Controls("lstbxSubjectSelected")

How do I achieve this.

Thanks

M
 
P

Peter Hewett

Hi Martin

I presuming the are ActiveX controls added from the "Control Toolbox"
toolbar and not FormFields.

You have to address controls explicitly. The Document object does not have
a Controls collection. Likewise if you examine the individual controls
you'll note that they don't have a Parent property either!

Otherwise use the ThisDocument class as you would a Form. Here's an example
using a ListBox control "lstColours" and a CommandButton control "btnDone".
All code is in the ThisDocument class module:

Private Sub btnDone_Click()
With lstColours
If .ListIndex < 0 Then
MsgBox "Nothing Selected"
Else
MsgBox "Selected colour: " & .Value
End If
End With
End Sub

Private Sub Document_New()
InitialiseForm
End Sub

Private Sub Document_Open()
InitialiseForm
End Sub

Private Sub InitialiseForm()
With lstColours
.AddItem "Red"
.AddItem "Green"
.AddItem "Blue"
.AddItem "Orange"
.AddItem "Yellow"
.AddItem "Violet"
End With
End Sub

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Martin

Sorry I forgot I had not actually answered your question! No you can't
assign any ActiveX control to a an MSForms object of type Control. The
ActiveX controls are individual ActiveX components (hence no Controls
collection and no Parent property). If you do need to do generic things
with a number of controls you can use:

Private Sub btnDone_Click()
Dim ctlItem As Object

Set ctlItem = lstColours
With ctlItem
If .ListIndex < 0 Then
MsgBox "Nothing Selected"
Else
MsgBox "Selected colour: " & .Value
End If
End With
End Sub

Because this is latebound code it's obviously not as fast or as safe as
making an explicit reference to a specific ActiveX control.

HTH + Cheers - Peter
 
M

Martin Newman

Thanks for the very fast reply.

What I actually wanted to do ws very similar things with a whole load of
controls so was hoping to be able to have a procedure and pass the control
as an argument - but I guess that is not possible.

Tanks again. I'll go back to my Saturday morning gardening now!
 
P

Peter Hewett

Hi Martin

You can as follows:

Private Sub DoSomething(byval objControl As Object)

' Do something with or to the control
With objControl
.Enabled = True
.Height = 100
.Width =40
End With
End Sub

You just have to make sure that the Properties/Methods you reference are
valid for the type of control you're passing to the procedure.

Good luck with the gardening. It's late and past my bedtime!

HTH + Cheers - Peter
 
M

Martin Newman

Yep - that works - obvioulsy having a senior momrnt here.

Thanks for your help
 

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