MS Word Option Button Collection
- Hi,
I am trying to use option buttons from the control toolbox in a word document.
I have set the groupname and they work fine but....
I then need to loop through the option button values in the group and I can not work out how to create the array or collection - I have tried the shapes property and the inlineshapes property of the active document but they have not worked.
I need code a bit alike below:
dim optBtn as msforms.optionbutton
for each optbtn in activedocument.??
msgbox optBtn.value
next optBtn
I can not work out what should be where the ?? are
Please help
Many thanks
Answers
Per one of our support engineers... try this code:
======Dim optBtn As Control
For Each optBtn In Me.Controls
If TypeName(optBtn) = "OptionButton" Then
If optBtn.GroupName = "test" Then
MsgBox optBtn.Value
End If
End If
Next optBtn
======
I hope this helps,
-brenda (ISV Buddy Team)- The code in the first response only works if the controls are on a userform. In your case, the controls are directly on the document. Assuming your controls are all in the main document (as opposed to in headers/footers, footnotes/endnotest, or textboxes), and assuming they are all inline and not floating, then the following code should work:
Dim oShape As Word.InlineShape
For Each oShape In ActiveDocument.InlineShapes
If oShape.OLEFormat.ProgID = "Forms.OptionButton.1" Then
MsgBox oShape.OLEFormat.Object.Value
End If
Next
Michael Herzfeld
Office Programmability Test Team
All Replies
Per one of our support engineers... try this code:
======Dim optBtn As Control
For Each optBtn In Me.Controls
If TypeName(optBtn) = "OptionButton" Then
If optBtn.GroupName = "test" Then
MsgBox optBtn.Value
End If
End If
Next optBtn
======
I hope this helps,
-brenda (ISV Buddy Team)- The code in the first response only works if the controls are on a userform. In your case, the controls are directly on the document. Assuming your controls are all in the main document (as opposed to in headers/footers, footnotes/endnotest, or textboxes), and assuming they are all inline and not floating, then the following code should work:
Dim oShape As Word.InlineShape
For Each oShape In ActiveDocument.InlineShapes
If oShape.OLEFormat.ProgID = "Forms.OptionButton.1" Then
MsgBox oShape.OLEFormat.Object.Value
End If
Next
Michael Herzfeld
Office Programmability Test Team Hi
I usethe folloing code get all the inline shapes
Microsoft.Office.Interop.Word.
InlineShapes shapes;shapes = (Microsoft.Office.Interop.Word.
InlineShapes)doc.GetType().InvokeMember("InlineShapes", BindingFlags.GetProperty, null, doc, null);But it is not retriving inline shapes in footnotes/endnotest, or textboxes. Is there a way to retrive the inlineshapes in the footnote . textbox.
Is there any specifice reason for this is not retriving the inlineshapes in the footnote and Textbox
Thanx
-Senthil
- You have to go through each footnote and look inside the range...
foreach (Footnote fn in Application.ActiveDocument.Footnotes)
{
foreach (InlineShape shape in fn.Range.InlineShapes)
{
// here your code using the shape obj.
}
}

