Macro That Sends an Email To Certain Addresses Based on Check/Unchecked Check Boxes

jmkonie

New Member
Joined
Nov 30, 2016
Messages
2
Ok, this is my first post. I have been able to use the forum to fix my issues with VB until this one.

Basically I would like a macro to decide which email, subject, and body content based on whether or not a check box is checked.

For instance:
Checkbox checked -> sends email to abc@abc.com with subject "abc has a new email" and body "abc test"
Checkbox unchecked -> send email to 123@abc.com with subject "123 has a new email" and body "123 test"

Here is what I have so far. I have the email function down, no issues, but when I introduce the If Then it throws an error and I guess I am not good at debugging yet so I cannot figure it out.

Sub Button()


Range("K41").Value = Environ("UserName")
' Inputs the person's username into cell K41.


Range("AE41").Value = Format(Now, "mm/dd/yyyy hh:mm:ss")
' Inputs the month, date, year, and exact time the document was approved in cell 41


Dim Path As String
Dim filename As String
filename = Range("AE1")
Path = "F:"
ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled


Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAttachments As Object

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set myAttachments = OutLookMailItem.Attachments

If Range("B37") = True Then
' So if I have the check box checked...


With OutLookMailItem
.To = "abc@abc.com"
' Who the email will be sent to


.Subject = "abc has a new email"
' What's written in the subject line


.Body = "abc test"
' What's written in the body of the email


myAttachments.Add "F:" & Sheets(1).Range("AE1").Value & ".xlsm"
' Automatically finds the file name based on what we named it earlier


Else


With OutLookMailItem
.To = "123@abc.com"
' Who the email will be sent to


.Subject = "123 has a new email"
' What's written in the subject line


.Body = "123 test"
' What's written in the body of the email


myAttachments.Add "F:" & Sheets(1).Range("AE1").Value & ".xlsm"
' Automatically finds the file name based on what we named it earlier


.Display
' This shows the email before sending it
End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing

End If


End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,

I've not tried your code, but just looking at it I think there are a couple of things which might help it to work:
(1) The first "With" statements does not have a corresponding "End With". You should add one in the line above "Else"
(2) The If section does not have a ".Display" statement, but the Else statement does
(3) The two lines of "Set OutLookMailItem = Nothing" and "Set OutLookApp = Nothing" should be below the "End If" statement.
 
Upvote 0
The first part of IF is missing "End With". Maybe you can put "With OutLookMailItem" and "End With" outside the IF statement.
 
Upvote 0
Got it. I added the "end with" after the first "with" statement, added the .Display statement, and added the "Set OutLookMailItem = Nothing" and "Set OutLookApp = Nothing" to the first "With" statement, kept them before the End If and it works great.

Now I need to figure out how to add some code to lock and unlock certain cells in the protected sheet.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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