Link to home
Start Free TrialLog in
Avatar of james_daley
james_daley

asked on

Referring to a many CheckBoxes for a FOR Statement, and performing the same test for each CheckBox.

I have a form in Outlook which has 10 check boxes. What i won't to do is perform the same test for each checkbox. If the checkboxes are Named CheckBox1 to CheckBox10, how do i refer to them so i can use a "For Checkbox1 to CheckBox10", do my test?
Avatar of David Lee
David Lee
Flag of United States of America image

Greetings, james_daley.

You need to set the checkboxes up as a control array.  The checkboxes then become CheckBox(0) through CheckBox(9).  You can then use either of these two approaches to itterate through them:

    For Each Item in CheckBox

or

    For x = 0 to 9
        CheckBox(x)
    Next

I've never tried to use a control array in an Outlook form though and am not certain they're supported.  I'll check that as soon as I can.

Cheers!
Avatar of james_daley
james_daley

ASKER

looking at other questions posted it seems that i need to name all the checkboxes the same but change the index value, then the code would work. problem is there is no property i can find in outlok that supports this index. any ideas?
Sorry about that, James.  I move back and forth between VB, VBA, and VBScript and get them mixed up sometimes.  Here's how to do this in code behind an Outlook form.  This is just and example, but you can see how the process works.

Sub CommandButton1_Click()
    Dim objForm, objControl, strName, intIndex
    'Get the Outlook form object
    Set objForm = Item.GetInspector.ModifiedFormPages("Message")
    For intIndex = 1 to 2
        'Build the control name
        strName = "Checkbox" & intIndex
        'Get the control from the Controls collection of the form object
        Set objControl = objForm.Controls(strName)
        'Set or test the control's value
        objControl.Value = Not objControl.Value
    Next
    Set objControl = Nothing
    Set objForm = Nothing
End Sub
ok so as i have no programming experience i'm a little lost. in particular the "Set objForm =..." puzzles me as it seems to stop there all the time and i don't understand the "message" part. what do i need to change in this code given my form is called UserForm1, Checkboxes named CheckBoxes1 to CheckBoxes 10, and the test i wanna perform is if the CheckBox is ticked insert name ( caption of checkbox) into text file, if not then don't write it. hopefully after going through 1 to 10 i get a list of names of who's ticked and this list should reset everytime the command button is hit. sorry about the lack of understanding!

thanks again
> in particular the "Set objForm =..." puzzles me
Ok, I'll explain.  That line of code is getting a particular form page.  We need to get the form object because it contains a collection of control objects, and that's what we need to access.  In my example I was using a message form and the checkboxes happened to be on the main page which is named "Message".  Substitute the name of the page the checkboxes are on in your custom form.  The code for the scenario you described above would be something like this:

'Get the form the controls are on
Set objForm = Item.GetInspector.ModifiedFormPages("MyPageName")
For intIndex = 1 To 10
    'Build the control name
    strName = "Checkbox" & intIndex
    'Get the control from the Controls collection of the form object
    Set objControl = objForm.Controls(strName)
    'If the box is checked
    If objControl.Value Then
        'Code to insert the checkbox caption in the text file
    End If
Next
it still get stuck at "MyPageName". what i have is a custom userform i've made called UserForm1, wth no pages, just 2 command buttons, some radio buttons which controls which check boxes start off ticked and unticked tick and a logo. even if i create 2 pages in the form called Page1 and Page2, if i reference the MyPageName as Page1 which all the controls are on i till get th same pop up "Run Time error 424: Object Required".
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial