Results 1 to 13 of 13
  1. #1
    rbiggs is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    23

    Send email with attachments

    I am trying to adapt the following code and it errors out in the highlighted row. I am not familiar with the syntax to set a DAO recordset could someone point me in the right direction or correct what I have.

    Thanks

    Private Sub btnEmail_Click()

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim strEMailMsg As String


    Dim strAttachmentPath As String
    Dim rsATT As DAO.Recordset
    Dim db As Database

    Set db = CurrentDb()
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

    With objOutlookMsg
    strEMailMsg = "Attached..........."
    .Subject = "Invoice Number" & [Invoice_Number]
    .body = strEMailMsg
    Set rsATT = CurrentDb.OpenRecordset("Invoice Query").Fields("Invoice_Attachments")
    If rsATT.BOF And rsATT.EOF Then
    GoTo noAttach
    Else
    Do Until rsATT.EOF
    strAttachmentPath = rsATT.Fields("FilePath")
    .Attachments.Add (strAttachmentPath)
    rsATT.MoveNext
    Loop
    End If
    noAttach:
    .Display
    End With
    Set rsATT = Nothing
    Set objOutlookMsg = Nothing
    Set objOutlook = Nothing

    End Sub

    Invoice Query is the dataset for the Form and the field name is Invoice_attachments. I get the error "Type Mismatch". The goal is to email all of the attachments related to an invoice.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    Set rsATT = CurrentDb.OpenRecordset("Invoice Query")

    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rbiggs is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    23
    If I take out the reference to the .fields like you suggested it breaks in the Else statement where it is setting the strAttachmentPath

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What's the error? FilePath is a field in the query? If it could be Null, you'll get an error trying to assign its value to a String variable. Only a Variant can take a Null.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    rbiggs is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    23
    Yes it is. It is not required since I may not have an attachment for every record how could I get around this. If I change strAttachmentPath to variant I get a Data Type Conversion error at .Attachments.Add (strAttachmentPath)

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Then you need to test for it. One way:

    Code:
    If Len(rsATT!FilePath & vbNullString) > 0 Then
      strAttachmentPath = rsATT.Fields("FilePath")
         .Attachments.Add (strAttachmentPath)
    End If
    Though looking at the code, it might make more sense to eliminate them from the query results.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    rbiggs is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    23
    Its still not working. I don't think I truly understand what the code is doing. Adding the If statement to account for 0 length filepaths works however the code is never seeing the attachments in the recordset.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you post the db, or a representative sample?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    rbiggs is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    23
    Here is what I am working with. The code is on the DispatchMain form. Thanks for your help

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    For starters, in this file there is no code associated with the button. The button is btnEmail and the code is:

    Private Sub btnEmail2_Click()

    I have to confess to assuming your field held the path to an attachment, not an embedded attachment. I don't like those because they bloat the database, and are unavailable anywhere else. I store a path, so users can open the file via other means but I can get at it from Access. It appears the code is intended for that, given the reference to path. I've never used the attachment data type, but I'll poke around and see if I can find out how to access them for an email.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Okay, a quick search would appear to suggest you can't email them directly. See the post here:

    http://answers.microsoft.com/en-us/o...f-fb31fb491ed9

    by Daniel Pineault, a few down from the top. Basically it sounds like you need to save the file to disk, use that path for the email attachment, and delete the file when you're done.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    rbiggs is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    23
    I have attached an updated copy. I think that it would be better not to use the embeded attachments and instead pass a string. I have changed the Invoice Query to create to Paths (Path and Path2) to a location on my computer. The code still seems to skip the .Attachments.Add thank you for your help

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You changed the logic flow. The attachment code is inside an If/Then that tests for EOF. In other words, it would only run when the recordset is empty, the opposite of what you want. Plus you have the GoTo right before it, so it would never run anyway. You had an "Else" in there before.

    As an aside, you test the first path field before attaching, but not the second. You'll likely get an error if the first field is populated and the second isn't.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How can I send an email from access???
    By Asma in forum Programming
    Replies: 2
    Last Post: 12-07-2011, 07:49 AM
  2. Send Report and Attachments in Email
    By Pimped in forum Programming
    Replies: 1
    Last Post: 06-21-2011, 02:51 AM
  3. Replies: 4
    Last Post: 04-13-2011, 10:11 AM
  4. send email to email addresses in database?
    By cnstarz in forum Access
    Replies: 5
    Last Post: 03-02-2011, 09:46 PM
  5. send email from a form
    By maxbre in forum Programming
    Replies: 4
    Last Post: 11-12-2010, 01:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums