Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2006

Tip: Looking for answers? Try searching our database.

Capturing the name of each optionbutton and checkbox in userform

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
red6000 - 20 May 2006 23:48 GMT
Hi

I have a userform (called 'ReqsFrm') that contains loads of optionbuttons
and checkboxes

I would like to create some code that writes the name/caption of each of the
elements of the form.  Now I can do it by manually producing the code, but
I'm sure there must be some sort of looping code that will do it for me.  So
I'm trying do to something like:

Dim OB as OptionButton
For each OB in ReqsFrm
Type name of OptionButton
Type caption of OtionButton
Next OB

and then the same for the checkboxes

Can anyone help.

Thanks
Greg Maxey - 21 May 2006 00:54 GMT
Red6000,

While I am not exactly sure I had to code the way I did, this may work:

Private Sub CommandButton1_Click()
Dim oButtonArray() As OptionButton
Dim oCheckboxArray() 'As CheckBox
Dim i As Long
Dim j As Long
Dim oControl As Control
For Each oControl In Me.Controls
 If TypeOf oControl Is MSForms.OptionButton Then
   ReDim Preserve oButtonArray(i)
   Set oButtonArray(i) = oControl
   i = i + 1
   GoTo SkipRest
 End If
 If TypeOf oControl Is MSForms.CheckBox Then
   ReDim Preserve oCheckboxArray(j)
   Set oCheckboxArray(j) = oControl
   j = j + 1
 End If
SkipRest:
Next oControl
For i = 0 To UBound(oButtonArray)
 ActiveDocument.Range.InsertAfter oButtonArray(i).Name & " " &
oButtonArray(i).Caption & vbCr
Next i
For j = 0 To UBound(oCheckboxArray)
 ActiveDocument.Range.InsertAfter oCheckboxArray(j).Name & " " &
oCheckboxArray(j).Caption & vbCr
Next j
Me.Hide
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi
>
[quoted text clipped - 17 lines]
>
> Thanks
red6000 - 21 May 2006 09:58 GMT
Thanks, that works great, what I would now like to do is expand the code to
run for each loaded UserForm (I have over 20 Userforms).

I did try the code below, but it seems to repeating he opitonbuttons and
checkboxes for each new UserForm it moves to:

Dim oButtonArray() As OptionButton
Dim oCheckboxArray() 'As CheckBox
Dim i As Long
Dim j As Long
Dim oControl As Control
Dim UF As Object
   Load xxFrm
   Load yyFrm
   Load zzFrm

For Each UF In UserForms

   For Each oControl In UF.Controls
   If TypeOf oControl Is MSForms.OptionButton Then
       ReDim Preserve oButtonArray(i)
       Set oButtonArray(i) = oControl
       i = i + 1
       GoTo SkipRest
   End If
   If TypeOf oControl Is MSForms.CheckBox Then
       ReDim Preserve oCheckboxArray(j)
       Set oCheckboxArray(j) = oControl
       j = j + 1
   End If
SkipRest:
   Next oControl
   For i = 0 To UBound(oButtonArray)
   ActiveDocument.Range.InsertAfter UF.Name & "," & oButtonArray(i).Name &
"," & oButtonArray(i).Caption & vbCr
   Next i
   For j = 0 To UBound(oCheckboxArray)
   ActiveDocument.Range.InsertAfter UF.Name & "," & oCheckboxArray(j).Name
& "," & oCheckboxArray(j).Caption & vbCr
   Next j

Next UF

> Red6000,
>
[quoted text clipped - 52 lines]
>>
>> Thanks
red6000 - 21 May 2006 10:10 GMT
Right I've got it to work by moving NextUF to aftert NextControl, but I've
had to lose prefixing the text with the UserForm Name.

> Thanks, that works great, what I would now like to do is expand the code
> to run for each loaded UserForm (I have over 20 Userforms).
[quoted text clipped - 95 lines]
>>>
>>> Thanks
red6000 - 21 May 2006 10:14 GMT
Okay, I want to go back to having the NextUF at the end, but just before
that I need code to reset and clear the original created array, is this
possibl?

Thanks for the help.

> Right I've got it to work by moving NextUF to aftert NextControl, but I've
> had to lose prefixing the text with the UserForm Name.
[quoted text clipped - 98 lines]
>>>>
>>>> Thanks
red6000 - 21 May 2006 10:23 GMT
Okay, all sorted now.

> Okay, I want to go back to having the NextUF at the end, but just before
> that I need code to reset and clear the original created array, is this
[quoted text clipped - 104 lines]
>>>>>
>>>>> Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.