VBA to send email

Justin_M

New Member
Joined
Feb 3, 2021
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
How would I turn this into looping through a list of email addresses contained in an excel worksheet? (same workbook)
How would I include the contents of a cell within the body of the email?


VBA Code:
Public Sub CreateNewMessage()
Dim objMsg As MailItem
Dim Selection As Selection
Dim obj As Object

Set Selection = ActiveExplorer.Selection

For Each obj In Selection

Set objMsg = Application.CreateItem(olMailItem)

 With objMsg
  .To = obj.SenderEmailAddress
  .Subject = "This is the subject"
  .Categories = "Test"
  .Body = "My notes" & vbcrlf & vbcrlf & obj.Body 
  .Display
' use .Send to send it automatically 

End With
Set objMsg = Nothing

Next

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Also, each email address is going to populate specific information which needs to be saved in an .xls and added as an attachment in the email.
 
Upvote 0
VBA Code:
Sub SendEmail()

Dim Source As String
Dim EmailApp As Outlook.Application 'To refer to outlook application

Set EmailApp = New Outlook.Application 'To launch outlook application

Dim EmailItem As Outlook.MailItem 'To refer new outlook email

Set EmailItem = EmailApp.CreateItem(olMailItem) 'To launch new outlook email

EmailItem.To = "hi@gmail.com"
EmailItem.CC = "hi@gmail.com"
EmailItem.BCC = "hi@gmail.com"
EmailItem.Subject = "Test Email From Excel VBA"

EmailItem.HTMLBody = "Hi," & vbNewLine & vbNewLine & "This is an email from Excel" & _
                     vbNewLine & vbNewLine & _
                     "Regards," & vbNewLine & _
                     "VBA Coder" 'VbNewLine is the VBA Constant to insert a new line

Source = ThisWorkbook.FullName
EmailItem.Attachments.Add Source

'EmailItem.Send
EmailItem.Display


End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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