Addressing radio buttons programmatically

D

Dave T

I'm trying to figure out how to address radio buttons, optionbuttons,
programmatically. These controls are embedded in a document, and I'd
like to use something similar to the way you can use
me.controls("nameofwhatever") to address them on a form. Any
suggestions?
 
J

Jay Freedman

Dave said:
I'm trying to figure out how to address radio buttons, optionbuttons,
programmatically. These controls are embedded in a document, and I'd
like to use something similar to the way you can use
me.controls("nameofwhatever") to address them on a form. Any
suggestions?

My most useful suggestion would be to give up on ActiveX controls embedded
in the body of the document, as they're more trouble than they're worth.
Create a userform instead. There, you can actually use the
Controls("nameofwhatever") syntax.

If you're stuck with ActiveX controls for some reason, the only usable
article about them is
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnword2k2/html/odc_activeX.asp.

In the ThisDocument module, you can simply address the controls by name,
such as OptionButton1 (or whatever name you assign in the Properties
dialog).

If you're going to iterate through the controls, the first thing you have to
know about your controls is whether they're inline or floating. Inline
controls are members of the ActiveDocument.InlineShapes collection, while
those that are floating are members of the ActiveDocument.Shapes collection;
they have to be dealt with separately. You must go through one of those
collections to get to the control's Name property, as in Listing 10 of the
article:

doc.Variables("PreviousControl").Value _
= doc.InlineShapes(1).OLEFormat.Object.Name

You have to know (or test) that InlineShapes(1) is an object that has an
OLEFormat property -- you can use a statement like

If doc.InlineShapes(1).Type = wdInlineShapeOLEControlObject Then

to avoid getting an error, or just use On Error Resume Next and then test
for Err.Number = 0.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Dave T

Thanks, I ended up using a 'for each' loop to address each of the
InLine objects, then differentiating between them using Instr:

<%
For Each thing In wrdDoc.inlineshapes
If InStr(LCase(thing.OLEFormat.Object.Name), "question") Then
'one of the radio buttons

If thing.OLEFormat.Object.Value = True Then
'we only care to know which ones have been selected
'groups without selected buttons don't concern us

End If
ElseIf InStr(LCase(thing.OLEFormat.Object.Name), "comment")
Then
'is a text box

If thing.OLEFormat.Object.Value <> "" Then
'user has comments

Else
'user has no comments

End If
End If
Next thing
%>
 

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