tick multiple checkboxes and send email

MachuPichu92

New Member
Joined
Feb 4, 2020
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone,

I almost finished my macro that automatically sends emails to different companies. Sometimes, I don't need to send emails to all companies included in the macro, but only to few.
I was thinking about an userform containing multiple checkboxes holding company name and SEND button. Click on SEND buttton would execute only those parts of macro that include chosen partner from ticked checkbox. Be aware that I have only one Sub.
Do you have any suggestions on how to define which part of code should be executed when checkbox is ticked?

I hope I described my issue understandable and thanks for you answers.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Here's how to loop through all the check boxes on the user form.
VBA Code:
Private Sub cmdSend_Click()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" And ctrl.Value = True Then
            ' do what's necessary for this checkbox if ticked
            ' example: display in a message box
            MsgBox ctrl.Caption
            '
        End If
    Next ctrl
End Sub
 
Upvote 0
Here's how to loop through all the check boxes on the user form.
VBA Code:
Private Sub cmdSend_Click()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" And ctrl.Value = True Then
            ' do what's necessary for this checkbox if ticked
            ' example: display in a message box
            MsgBox ctrl.Caption
            '
        End If
    Next ctrl
End Sub

First of all, thank you for you answer.
I have already thought about this kind of code, but it seems it would not be compact with my existing code.

Please see below an example of an email for 1 of companies:

*********

With OutLookMailItem1
.Display
.To = "myrecipient"
.CC = "ccrecepient"
.Subject = "Quantity confirmation" & " " & (Format(DateAdd("M", -1, Now), "MMMM")) & " " & Year(Now)
.HTMLBody = Message & .HTMLBody

QCFile = Dir(QCPath & "*_COMPANY_*" & QCMonthYear & ".pdf")

Do While QCFile <> "" '**search for multiple attachments
DoEvents
myAttachments1.Add (QCPath & QCFile)
QCFile = Dir
Loop

att1 = OutLookMailItem1.Attachments.Count
If att1 > 2 Then 'because 2 objects are already defined
'.Send
Else
OutLookMailItem1.Delete
End If

End With

********************

I have 8 OutLookMailItems written in such way and I'm wondering if there is an option to execute code from line "With OutLookMailItem1 " if Checkbox for this company is ticked?

thanks in advance
 
Upvote 0
I'm not able to test anything but maybe try
VBA Code:
Private Sub cmdSend_Click()
    Dim ctrl As Control, myrecipient As String
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" And ctrl.Value = True Then
            myrecipient = ctrl.Caption
            Call NameOfYourOtherSub(myrecipient)
        End If
    Next ctrl
End Sub
and put myrecipient as string inside the brackets of your other sub like
Sub YourOtherSub(myrecipient As String)
and remove the double quotes from the .To line
 
Upvote 0
I'm not able to test anything but maybe try
VBA Code:
Private Sub cmdSend_Click()
    Dim ctrl As Control, myrecipient As String
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" And ctrl.Value = True Then
            myrecipient = ctrl.Caption
            Call NameOfYourOtherSub(myrecipient)
        End If
    Next ctrl
End Sub
and put myrecipient as string inside the brackets of your other sub like
Sub YourOtherSub(myrecipient As String)
and remove the double quotes from the .To line

Thanks for you help, but I found another way to deal with my issue:

VBA Code:
If CheckBox1.Value = True Then

        
        With OutLookMailItem1
        .Display
        .To = "myrecipient"
        .CC = "ccrecepient"
        .Subject = "Quantity confirmation" & " " & (Format(DateAdd("M", -1, Now), "MMMM")) & " " & Year(Now)
        .HTMLBody = Message & .HTMLBody

        QCFile = Dir(QCPath & "*_COMPANY_*" & QCMonthYear & ".pdf")

        Do While QCFile <> "" '**search for multiple attachments
        DoEvents
        myAttachments1.Add (QCPath & QCFile)
        QCFile = Dir
        Loop

        att1 = OutLookMailItem1.Attachments.Count
            If att1 > 2 Then 'because 2 objects are already defined
            '.Send
            Else
            OutLookMailItem1.Delete
            End If

        End With
MsgBox ("1")
Else
End If


Best regards
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top