I'm having a problem attaching a subform to an e-mail message. Before, I was sending the e-mail through outlook using DoCmd, but recently re-wrote the code to send through an SMTP server in order to avoid the annoying warning message. I want to attach a subform located with in the same database, called "5-Day Ticket Notification Subform", but I keep getting Runtime Error 438. I'm not sure if using .Attachments.Add is causing the error, or what. I've searched forums trying to find a solution, but have had no luck. Any help y'all can give will be much appreciated!
' Sends the e-mail
Set .Configuration = Conf
.To = Address
.CC = ""
.BCC = ""
.From = """Name"" <e-mail address>"
.Subject = "5-Day Notification"
.Attachments.Add Forms![5-Day Ticket Notification Subform]
.TextBody = StringBody
Ok, so I've done that, but now it's telling me that it can't find the form. I've checked the name many times, and copy and pasted the name of the form and subform into my vba code, so I am SURE there is no typo.
' Declare module-level variables
Dim Msg As Object
Dim Conf As Object
Dim StringBody As String
Dim Flds As Variant
Dim Address As Field
Dim Name As Field
Dim a As Control
' Assign object references
Set Msg = CreateObject("CDO.Message")
Set Conf = CreateObject("CDO.Configuration")
Set Address = r.Fields("E-Mail Address")
Set Name = r.Fields("First Name")
Set Flds = Conf.Fields
Set a = Forms![5-Day Ticket Notification]![5-Day Ticket Notification Subform]
I'm getting "Run Time error '2465': Field Ticket Tracking Database can't find the field '5-Day Ticket Notification Subform' referred to in your expression."
Notice, it says "can't find the FIELD...", so I'm wondering if I haven't referred to the form/subform properly? If I put quotes around the form and subform name in the vba code, it returns the same error, but says that it "can't find the form..."
I figured out a solution. When the user clicks the button to send the e-mail, the subform is automatically exported and later attached to the e-mail message in the "With msg" loop. It works like a charm
Thanks for y'all's help!
When sending attachments using an SMTP server (CDO e-mail), you have to use .AddAttachment (FileName) INSTEAD of .Attachments.Add (FileName).
.Attachments.Add is used for Outlook messages. I found this out the hard way (many many hours of forum surfing, and reading up on VBA language specifications), so I'll pass this knowledge on to anyone who might be experiencing a similar problem in hopes to save them some valuable time.
Last edited by Chad Jackson; January 3rd, 2013 at 08:56 AM.