Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2003
    Posts
    106

    Unanswered: Email attachments from VB

    Hi

    Can anyone help please?

    I would like to email attachments from my code. The attachments are Excel spreadsheets. They have to be emailed as spreadsheets, not tables etc.

    Thanks
    sugarflux

  2. #2
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    here's my version of a "universal" outlook mailer which i use all over the place - it handles (optional) attachments and lurks in a global module in most of my apps.

    my apologies to all the other folks who involuntarily contributed code snippets to this func()


    Code:
    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
        
    
        On Error GoTo err_izyMailer
    
        
        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
        
    exit_izyMailer:
        Set objOutDist = Nothing                    'tidy up
        Set objOutFile = Nothing
        Set objOutMail = Nothing
        Set objOutlook = Nothing
        
        Exit Function                               'and exit
     
    err_izyMailer:
        izyMailer = False                           'failure return
        MsgBox Err.Description, vbCritical, "izyMailer Error"
        Resume exit_izyMailer
     
    End Function
    izy
    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
  •