Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2003

    Unanswered: Help: How do I set up the VBcode to Attach a file when sending an email through Form

    I am having trouble figuring the code syntax. I want to be able to attach a document when I send an email through a form. Here is the code I have so far, I got it from here, maybe the original author of the code could help me with being able to attach a file when emailing. Thank you.

    Private Sub SendEmailWork_Click()
    Dim email As Outlook.MailItem
    Set email = CreateObject("Outlook.Application").CreateItem(olM ailItem)
    email.To = [Forms]![DataEntry]![CompanyEMail]
    email.Subject = EmailSubject
    email.Body = EmailMessage
    End Sub

  2. #2
    Join Date
    Dec 2002
    Préverenges, Switzerland
    there are dozens of different ways to do this: here's my version (which unfortunately has almost nothing in common with your work so far).

    Public Function izyMailer(isDraft As Boolean, isTo As String, isSubj As String, isBody As String, Optional isFile As String = "NONE") As Boolean
        'function requires:
        '   isDraft TRUE saves the mail in drafts, FALSE sends immediate
        '   isTo    the recipient's mail address (addrFirst; addrMiddle; etc; addrLast   if a distribution list)
        '   isSubj  the mail subject
        '   isBody  the mail body text
        'and optionally accepts
        '   isFile  the drive:/path/name.extension of a file to attach
        'caller is responsible for validating isFile
        'function returns TRUE on success, else FALSE
        '  If Not izyDebug Then
            On Error GoTo err_izyMailer
        '  End If
        Dim objOutlook As Outlook.Application
        Dim objOutMail As Outlook.MailItem
        Dim objOutDist As Outlook.Recipient
        Dim objOutFile As Outlook.Attachment
        Set objOutlook = CreateObject("Outlook.Application")
        Set objOutMail = objOutlook.CreateItem(olMailItem)
        Set objOutDist = objOutMail.Recipients.Add(isTo)        'make distribution list 
        objOutDist.Type = olTo
        objOutMail.Subject = isSubj                        'make subject
        objOutMail.Body = isBody                            'make message body
        If Not isFile = "NONE" Then Set objOutFile = objOutMail.Attachments.Add(isFile)   'attachment
        objOutMail.Save                             'save as draft
        If Not isDraft Then objOutMail.Send         '...and possibly send
        izyMailer = True                            'success return
        Set objOutDist = Nothing                    'tidy up
        Set objOutFile = Nothing
        Set objOutMail = Nothing
        Set objOutlook = Nothing
        Exit Function                               'and exit
        izyMailer = False                           'failure return
        MsgBox Err.Description, vbCritical, "izyMailer Error"
        Resume exit_izyMailer
    End Function

    the save-as-draft is useful to avoid Mr. Gates' security crap with the 5-sec delay per message... if you could provide me with some outlook vb code that will send everything in the Drafts folder: great!

    currently using SS 2008R2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts