VBA code to send document by email

sb003848

Board Regular
Joined
Sep 17, 2009
Messages
66
In a Macro, what code would I need to use if I wish to send the document by email accroding to the following:

If "E12" = 1 the send the document by email to:

TO FIELD:
Email address found in cell "B30"

CC FIELD:
orders@email.com

BCC FIELD:
If SUM of cells "E9"+"E10"+"E11"+"E13"+"E14" equals or greater than 1, send email to abc@email.com
If cell "E12" equals 1, send email to def@email.com

What do I need to do in order to make this happen?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Went on Google and after trying multiple things, I found my answer:

Sub SendMail()
If Application.Sum(Range("E9:E11,E13:E17")) > 0 And Range("E12") = 0 Then
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = Range("B30")
.cc = abc@email.com
.Subject = "Message sent to abc@email.com"
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub
End If

If Range("E12") = 1 And Application.Sum(Range("E9:E11,E13E17")) = 0 Then
Dim OutApp1 As Object
Dim OutMail1 As Object

Set OutApp1 = CreateObject("Outlook.Application")
OutApp1.Session.Logon
Set OutMail1 = OutApp1.CreateItem(0)

On Error Resume Next
With OutMail1
.To = Range("B30")
.cc = "def@email.com"
.Subject = "Message for def@email.com"
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0

Set OutMail1 = Nothing
Set OutApp1 = Nothing
Exit Sub
End If

If Application.Sum(Range("E9:E11,E13:E17")) > 0 And Range("E12") = 1 Then
Dim OutApp2 As Object
Dim OutMail2 As Object

Set OutApp2 = CreateObject("Outlook.Application")
OutApp2.Session.Logon
Set OutMail2 = OutApp2.CreateItem(0)

On Error Resume Next
With OutMail2
.To = Range("B30")
.cc = "abc@email.com; def@email.com"
.Subject = "Message for abc@email.com and def@email.com"
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0

Set OutMail2 = Nothing
Set OutApp2 = Nothing
Exit Sub
End If
End Sub
 
Upvote 0
Hi,

Maybe you could shorten that.

Rich (BB code):
If Application.Sum(Range("E9:E11,E13:E17")) > 0 And Range("E12") = 0 Then
ccTo = "abc@email.com"
MailSubject = "Message sent to abc@email.com"
End If
 
If Range("E12") = 1 And Application.Sum(Range("E9:E11,E13:E17")) = 0 Then
ccTo = "def@email.com"
MailSubject = "Message for def@email.com"
End If
 
If Application.Sum(Range("E9:E11,E13:E17")) > 0 And Range("E12") = 1 Then
ccTo = "abc@email.com; def@email.com"
MailSubject = "Message for abc@email.com and def@email.com"
End If
 
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Range("B30")
.CC = ccTo
.Subject = MailSubject
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub

What happens if E12=0 and the Sum = 0?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,766
Members
452,940
Latest member
rootytrip

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