Results 1 to 6 of 6
  1. #1
    behnam is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    72

    How to attach file from attachment field into email using docmd.sendobject method.


    Hi, i have an attachment field for a table i created. I want to send an email out. I know how to do it using the docmd.sendobject method but cant figure out how to attach the file in the attachment field of the record. Can someone give me any ideas, thanks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    50,028
    Can't do this with SendObject.

    The attachment item must first be saved externally then code will attach to an Outlook email object.

    Common topic. Here is one thread about using email object: https://www.accessforums.net/macros/...rts-46472.html

    Discussions about saving attachment to external location:
    https://www.accessforums.net/access/...orm-42994.html
    https://www.accessforums.net/program...ent-23686.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    behnam is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    72
    I try the following code and others similar

    Code:
    Dim olApp As Outlook.Application 
        Dim olMail As Outlook.MailItem 
         
         'get application
        On Error Resume Next 
        Set olApp = GetObject(, "Outlook.Application") 
        If olApp Is Nothing Then Set olApp = New Outlook.Application 
        On Error Goto 0 
         
        Set olMail = olApp.CreateItem(olMailItem) 
        With olMail 
            .Subject = "My email with attachment" 
            .Recipients.Add "name@host.com" 
            .Attachments.Add "c:\test.txt" 
            .Body = "Here is an email" 
            .Display 'This will display the message for you to check and send yourself
             '.Send ' This will send the message straight away
        End With 
    
    but I get an error message saying "User defined type not defined" on the first line. I know the code it correct but i must be missing something.

  4. #4
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Here is an Example: Simple Version:
    DoCmd.SendObject acSendNoObject, "FormTyp", acFormatHTML, [EMail], , , , "Sent From Me To You", True, ""
    Of course where EMail is the field name.

    Fancy Version:


    Dim strToWhom As String
    Dim strMsgBody As String
    Dim strSubject As String
    Dim strBcc As String
    Dim strrptName As String
    Dim strextension As String

    strToWhom = Me.EmailTo
    strSubject = Me.EmailSubject
    strBcc = Me.cbobcc
    strMsgBody = Me.EmailMessage





    If Me.lstReports > "" Then 'ListBox of Reports
    DoCmd.SendObject acSendReport, Me.lstReports, stroutputType, strToWhom, , strBcc, strSubject, strMsgBody, True
    Else
    DoCmd.SendObject , , , strToWhom, , strBcc, strSubject, strMsgBody, True
    End If
    HTH

  5. #5
    behnam is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    72
    Thanks burrina, but i need to add an attachment that is already externally inside the database in a field called email

    Code:
    Set objOutlook = CreateObject("Outlook.application")
    Set objEmail = objOutlook.CreateItem(olMailItem)
    With objEmail
        .To = "Behnam Torabi"
        .CC = ""
        .Subject = "Daily Email Processed "
        .Body = "Hi,"
        .Attachments.Add = email.Value
            
        .Display
    End With
    Set objOutlook = Nothing
    everything here works but the attachment.add component. Can someone help me attach the attachment in the attachment field i created called email, thanks.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    50,028
    See post 2.

    Can't add the file directly from the field. The file must be saved external from the database.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 31
    Last Post: 08-15-2012, 03:33 PM
  2. Replies: 10
    Last Post: 03-26-2012, 08:17 PM
  3. VBA Send Email using DoCmd.SendObject
    By malamute20 in forum Programming
    Replies: 15
    Last Post: 10-05-2011, 12:44 PM
  4. Replies: 1
    Last Post: 11-17-2010, 11:24 AM
  5. Email sent via DoCmd.SendObject
    By silverback in forum Programming
    Replies: 0
    Last Post: 10-29-2009, 06:26 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 - Senior Forums