loop thru controls

J

James

Hello NG,
Can someone please help me understand how to loop through the controls on a
document?
I have a page with several OptionButtons on it and I want to be able to
clear all the buttons with a macro, here is what I have done, but it doesn't
work.
=======Begin Code======
Private Sub ClearButtons()
Dim bt As Control
For Each bt In ActiveDocument
If TypeOf bt Is OptionButton Then
bt.Value = False
End If
Next
End Sub
========End Code=======

Any help would be greatly appreciated
Thanks
James
 
M

Martin Seelhofer

Hi James (Bond?)
I have a page with several OptionButtons on it and I want to be able to
clear all the buttons with a macro, here is what I have done, but it
doesn't work.

Well, your code has several shortcomings:

1. In a For Each loop, you'll have to tell VBA through which collection
of objects you want to loop. Instead of ActiveDocument, you'll have
to supply VBA with e.g. ActiveDocument.Bookmarks (looping through
bookmarks), ActiveDocument.Shapes (looping through freely positionable
objects) or ActiveDocument.InlineShapes (looping through objects
embedded in the text). In your case, I assume that your Option Buttons
are embedded in the text. Therefore, you might have to use the
Inlineshapes-collection.

2. OptionButtons are ActiveX-controls packaged into wrapper objects
and as such embedded like every other object (image, drawing, ...) into
the text as InlineShapes. To get through to the ActiveX-control itself
(and its properties), you'll have to access the Object-property of the
wrapper object (shape.OLEFormat). See the example below.

Here's an example which shows you how to deal with the mentioned
problems and objects:

Dim opt As OptionButton
Dim ishp As InlineShape
For Each ishp In ActiveDocument.InlineShapes
' consider ActiveX-controls only:
If ishp.Type = wdInlineShapeOLEControlObject Then
' get through to the ActiveX-control itself
Set opt = ishp.OLEFormat.Object
' finally adjust the value to false
opt.Value = False
End If
Next


Cheers,
Martin
 
J

James

Martin,
Thanks for the help, I had no idea about all the different kinds of objects.
The loop works great for the first time through, but the second time I get
an error message saying Run-time Error '13' Type Mismatch.
I tried to change the object to something else and it won't work at all, so
you're right about the type of object as an InlineShape.
Any idea as to why it only loops once?
The only objects I have on the page are the option buttons.
Anyway thanks for the help, and the heads up on the many different type of
objects.
James
 
J

James

Martin,
I need to change the "I only have option buttons" on the document to, I also
have two labels, and one of the labels is the second object, that's where
it's crashing.
I put in an "On Error Resume Next" statement, and it seem to work now
James
(not as smart as Bond)
 

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