PDA

View Full Version : VBA Email Attachment



brorick
10-22-2007, 07:18 AM
I have successfully created the necessary code to send an email from Access. The problem I am having is trying to send an attachment. I have a field that identifies the attachment in my table (rst![Attachments]). The user is given the option to include an attachment only when necessary, therefore there may be many records that do not include an attachment. My code works perfectly when there is an attachment. :doh: I am trying to compensate for the senario when there is no attachment. I am using the following code and I am not having any success. I am hoping someone has an excellent example. Your help is greatly appreciated. Thank you in advance.

Dim objOutlookMsg As Outlook.MailItem
Dim strAttach As String
Dim db As DAO.Database
Dim rst As DAO.Recordset


Set db = CurrentDb()
Set rst = db.OpenRecordset("Qry_Bulletins_Report")


If Not IsNull(strAttach) Then
strAttach = rst![Attachments]
objOutlookMsg.Attachments.Add strAttach
End If

mattj
10-22-2007, 07:48 AM
In your current code, your testing whether strAttach isn't Null. The problem is that according to this code, no value is assigned to strAttach until after the If statement. Therefore, strAttach is always Null when it's tested.
Try testing the field in the recordset instead of the variable.
Dim objOutlookMsg As Outlook.MailItem
Dim strAttach As String
Dim db As DAO.Database
Dim rst As DAO.Recordset


Set db = CurrentDb()
Set rst = db.OpenRecordset("Qry_Bulletins_Report")


If Not IsNull(rst!Attachments) Then
strAttach = rst![Attachments]
objOutlookMsg.Attachments.Add strAttach
End If

HTH

Matt

brorick
10-22-2007, 01:36 PM
MattJ, thank you for your response. I discovered the same thing shortly after my post. Here is the solution.

If Not IsNull(rst![Attachments]) Then
strAttach = rst![Attachments]
End If


If strAttach <> "" Then

objOutlookMsg.Attachments.Add strAttach
End If