PDA

View Full Version : Looping Controls



mabernico
11-02-2007, 04:39 PM
I have a document in Word 2003 with two different sets of option (radio) buttons that I need to be able to clear individually. The best I can describe this is in code.

I need something similar to this to work.


Sub Clear()

Dim x as integer

For x = 1 to 100

OptionButton(x).Value=False

Next x

End Sub

I would like to loop from OptionButton1 to OptionButton100 and change each value to false.


Thanks in advance.

TonyJollans
11-02-2007, 05:13 PM
There is no real way to do what you want directly. Depending on the way they are set up, the option buttons may belong to either the Shapes or the InlineShapes Collection. Both these collections may, of course, contain other objects as well.

This should give you a start:
For Each Sh In ActiveDocument.Shapes
If Sh.Type = msoOLEControlObject Then
If TypeOf Sh.OLEFormat.Object Is MSForms.OptionButton Then
Sh.OLEFormat.Object.Value = False
End If
End If
Next
For Each ISh In ActiveDocument.InlineShapes
If ISh.Type = wdInlineShapeOLEControlObject Then
If TypeOf ISh.OLEFormat.Object Is MSForms.OptionButton Then
ISh.OLEFormat.Object.Value = False
End If
End If
Next

mabernico
11-03-2007, 04:16 PM
The problem with that solution is that it works for ALL OptionButtons

I have 2 sections full of these OptionButtons.

One section has only 36 which isn't too bad to program line by line, but the other section has 155. Using cut and paste, and then search and replace makes this bearable, but the loop would be much more convenient.

TonyJollans
11-03-2007, 04:34 PM
So what are your criteria for wanting to reset them?

mabernico
11-03-2007, 08:02 PM
I want one cmdButton to clear the first 36 and another cmdButton to clear the 155. This isn't being done within a form this actually part of the document itself. It can't be done in forms because this is a technical publication as well as an "interactive" document.

The state of reset that I would like is no circle filled in any circle i.e. all circles empty.

The first 36 belong to a group of 12 steps each with 3 outcomes and the 155 belong to 31 steps each with five outcomes. Each step's optionbuttons are set up as groups.

I want the buttons to reset these so that when the steps are repeated the circles are empty so that the user knows what steps have been done on this particular go around.

TonyJollans
11-04-2007, 01:43 AM
Thank you.

Working with controls on a document can be awkward as they can be set up in different ways. Sometimes the visible difference is trivial, or even non-existent, but the view in the object model available to VBA may be very different. So I need a bit more detail or, better if you can do it, a sample document posted here.

How are your option buttons grouped? By settting a GroupName in the properties, or are they inside other controls?

In your first post you asked if you could loop through them by name. You can't but you can check the name whilst looping through them all so, for example, to only reset numbers 1 to 36 you might try ...

For Each ISh In ActiveDocument.InlineShapes
If ISh.Type = wdInlineShapeOLEControlObject Then
If TypeOf ISh.OLEFormat.Object Is MSForms.OptionButton Then
If CInt(Mid(ISh.OLEFormat.Object.Name, 13)) <= 36 Then
ISh.OLEFormat.Object.Value = False
End If
End If
End If
Next

Similarly you can check other details such as the Group name.

If the buttons are in different Sections you could restrict the loop by Section ...

For Each ISh In ActiveDocument.Sections(1).Range.InlineShapes
' etc...

There might be other ways, as I said, depending on your particular arrangement.

fumei
11-06-2007, 12:05 AM
Hopefully they ARE in different Sections, which would make it a lot easier!

Tony is right of course, while the visible difference may be nothing, the differences within the object model, and what needs to be done by code can be very different.

A sample doc would be best, but if not, you need to precisely describe how the controls were placed. Have you renamed them at all?

fumei
11-06-2007, 12:10 AM
Also, please try and be very very clear when you post.

From you post #1:
"I would like to loop from OptionButton1 to OptionButton100 and change each value to false."

From post #2"
"The problem with that solution is that it works for ALL OptionButtons"

Yes, it would. You mentioned 100 buttons, your original code process all 100...so....

Which is why Tony tried again, by asking what is criteria for clearing. Which, you did.

Frankly, I am having difficulty understanding this:

"The state of reset that I would like is no circle filled in any circle i.e. all circles empty. "

I am not exactly sure what that means.