Add email attachment as variable for date

as586

New Member
Joined
Nov 28, 2010
Messages
12
Hi,

I am trying to add a report as an attachment to an email, and this report is updated and sent daily. Therefore I would like to be able to attach the report that is up to date. For example the file name for the report today and date formate would be "Report 11.10.2020". I have tried making the attachment a variable, however I think i have defined it incorrectly (I am getting a 'Path does not exist' error message). I think maybe the date at the end of the file name is not recognised. Below is currently what I have, could anyone please help? Thanks!


Sub MarketReportEmails()

Dim Signature$
Dim olApp As Object
Dim olMail As Object
Dim attachment As String
Dim dte As Date

Set olApp = GetObject(, "Outlook.Application")
Set olMail = olApp.CreateItem(0)

dte = Date

Set attachment = "C:\Users\ans\Documents\Daily report " & dte & ".pdf"

With olMail

.Subject = "Daily Report " & Date

.GetInspector
Signature = olMail.HTMLBody
olMail.HTMLBody = "<body style='font-family:calibri;font-size:11pt'>example" _
& "<br>" _
& "<br>" & Signature

.attachments.Add (attachment)

olMail.Display

End With

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try:

VBA Code:
Sub MarketReportEmails()

Dim Signature$
Dim olApp As Object
Dim olMail As Object
Dim attachment As String
Dim dte As Date

Set olApp = GetObject(, "Outlook.Application")
Set olMail = olApp.CreateItem(0)

dte = Date

attachment = "C:\Users\ans\Documents\Daily report " & Format(dte, "DD.MM.YYYY") & ".pdf"

With olMail

.Subject = "Daily Report " & Date

.GetInspector
Signature = olMail.HTMLBody
olMail.HTMLBody = "<body style='font-family:calibri;font-size:11pt'>example" _
& "<br>" _
& "<br>" & Signature

.attachments.Add (attachment)

olMail.Display

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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