-
December 31st, 2012, 10:30 AM
#1
[RESOLVED] Attaching Sub Form to SMTP Message
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!
Code:
' Sends the e-mail
With Msg
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
.Send
End With
-
December 31st, 2012, 01:44 PM
#2
Re: Attaching Sub Form to SMTP Message
Format the .From address, and then pass it to MSG class/form BEFORE you call it.
-
January 2nd, 2013, 11:57 AM
#3
Re: Attaching Sub Form to SMTP Message
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.
Code:
' 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..."
Either way, I'm getting the same error code.
-
January 2nd, 2013, 02:30 PM
#4
Re: Attaching Sub Form to SMTP Message
Shoudln't that be a period between the form and sub form rather than a ! ?
Always use [code][/code] tags when posting code.
-
January 2nd, 2013, 02:45 PM
#5
Re: Attaching Sub Form to SMTP Message
Tried that, and I'm still getting the same error code, only it says "can't find the field 'l'..."
-
January 2nd, 2013, 04:07 PM
#6
Re: Attaching Sub Form to SMTP Message
Can't really call an ACCESS form in a VB app. Should be accessing the DATA from the database directly.
-
January 3rd, 2013, 09:54 AM
#7
Re: Attaching Sub Form to SMTP Message
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!
P.S.
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 09:56 AM.
Reason: Typo
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|