PDA

View Full Version : Sending emails with attached csv in Access 2010 to Outlook



Amplify
12-04-2012, 05:00 PM
Hello everyone, I continue to stumbled upon this forum and so I decided to jump in. Thanks everyone for being here to help.

The title of my post is popular but surprisingly I cannot find a solution.
I have the below code to send email containing an attachments out of Access 2010 to Outlook.

The problem is I can only attached files with fixed names. My file names have to all be the same, otherwise the code fails. I would love to simply grab any CSV file in the specified folder and attached it. This is because my files name changes each time using a date/time stamp.

I also do not know how to make the code fail if the folder is empty or the directory changes. It would be great for it to just skip to the next sub rather than crashing. I don't need a message for this.

Private Sub sendEmail()
Dim strGetPath As String
Dim strGetFile As String

strGetPath = "C:\Database\datafolder\*.csv"

strGetFile = Dir(strGetPath)

Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "bob@builder"
''.cc = ""
''.bcc = ""
.Subject = "text here"
.HTMLBody = "text here"
.Attachments.Add(strGetFile & "*.csv")
.Send
End With

This is way beyond my reach.

Thanks again!