Solved

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

Posted on 2006-06-21
7
204 Views
Last Modified: 2010-04-08
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?
0
Comment
Question by:james_daley
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 16958510
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!
0
 
LVL 1

Author Comment

by:james_daley
ID: 16965885
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?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16972693
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
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Author Comment

by:james_daley
ID: 16989652
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
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17006291
> 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
0
 
LVL 1

Author Comment

by:james_daley
ID: 17007172
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".
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 17007961
You never mentioned that this is userform, so I was under the impression that this was a custom Outlook form.  In Outlook "form" represents a contact, task, appointment, message, etc. form.  No, userforms don't have pages.  Doing this in a userform will look like this instead.

Dim objForm As UserForm

Private Sub CommandButton1_Click()
    Dim strMessage As String, _
        strName As String, _
        objControl As Control, _
        intIndex As Integer
    For intIndex = 1 To 10
        strName = "Checkbox" & intIndex
        Set objControl = objForm.Controls(strName)
        If objControl.Value Then
            'Code to insert the checkbox caption in the text file
        End If
    Next
    Set objControl = Nothing
End Sub

Private Sub UserForm_Initialize()
    Set objForm = Me
End Sub
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
Outlook Free & Paid Tools
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now