Solved

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

Posted on 2006-06-21
7
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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: …

623 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