Solved

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

Posted on 2006-06-21
7
207 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
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: …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

860 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