Automating Email VBA

TimTim259

New Member
Joined
Sep 26, 2022
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
So I've been tryin to automate email sending. I have a pdf I want to be emailed to every person in a column every time an email is added. I am struggling with adding the pdf I want to send with .attachment.add then the "location"

Here is what i have written so far.


Sub SendEmail()
'
' SendEmail Macro
'

'

Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem

Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)

With OutlookMail
.BodyFormat = olFormatHTML
.Display
.HTMLBody = "This is a test"

.To = "testemail@gmail.com"
.CC = ""
.BCC = ""
.Subject = "TEST !"
.Attachments.Add = "Location"
.Send
End With
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi,

code line
Code:
.Attachments.Add = "Location"
should read something like
Code:
  .Attachments.Add "C:\Daten\2022-09-26.png"
You cannot add the string "Location" but would need to give details for the path and the full name of the attachment.

VBA Code:
Sub SendEmail_re()
'
' SendEmail Macro
'

'Late Binding, no Reference to Outlook needed

Dim objOlApp          As Object
Dim objOlMail         As Object
Dim strAttachment     As String

Set objOlApp = CreateObject("Outlook.Application")
Set objOlMail = objOlApp.CreateItem(0)
strAttachment = "C:\Daten\2022-09-26.png"     'change to suit

With objOlMail
  .BodyFormat = olFormatHTML
  .HTMLBody = "This is a test"
  .To = "testemail@gmail.com"
  .CC = ""
  .BCC = ""
  .Subject = "TEST !"
  .Attachments.Add strAttachment
  .Display
  .Send
End With

Set objOlMail = Nothing
Set objOlApp = Nothing

End Sub
You could alter the path to the attachment by referring to a cell in the worksheet.

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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