Results 1 to 5 of 5
  1. #1
    Sephaerius is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    62

    Sending multiple emails

    So I've got a database that exports 6 different Excel docs which need to be distributed to different departments. I'm looking for a best practice regarding my vb in sending the emails, rather than just repeating a chunck of code 6 times. Here's what I've got. It works fine - but since I will have err1.xls through err6.xls I'd like to repeat the task of emailing them for each document. Additionally, each document goes to a different email address. Thank you.

    Code:
    Public Function email()
        Dim objOutlook As Outlook.Application
        Dim mail As Outlook.MailItem
        
        strFile = "<a href=""\\path\err1.xls"">Click here</a>"
        
        Set objOutlook = GetObject(, "Outlook.Application")
        
        If objOutlook Is Nothing Then
            Set objOutlook = CreateObject("Outlook.Application")
        End If
        
        Set mail = objOutlook.CreateItem(olMailItem)
        On Error Resume Next
        With mail
            .To = emailto
            .CC = ""
            .BCC = ""
            .Subject = ""
            .HTMLBody = "<p>New records have been added to your spreadsheet </p>" & vbNewLine & vbNewLine & "<p>Thank you,</p>" & vbNewLine & strFile
            .Send
        End With
        Set mail = Nothing
        Set objOutlook = Nothing
    End Function


  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
    One option would be to pass the file name (or number, or complete path, etc) and email address to the function, which would make it more generic.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Sephaerius is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    62
    Quote Originally Posted by pbaldy View Post
    One option would be to pass the file name (or number, or complete path, etc) and email address to the function, which would make it more generic.

    How would that be done?

  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
    Public Function email(strPath As String, strEmail As String)

    then use those 2 variables in the code. Another option is a For/Next loop inside the function, but since each goes to a different email address that seems impractical.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    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. Sending several emails causes Outlook crash
    By mercapto in forum Programming
    Replies: 3
    Last Post: 06-14-2013, 07:30 AM
  2. Generating and sending emails through Access
    By crowegreg in forum Programming
    Replies: 9
    Last Post: 06-10-2013, 08:35 AM
  3. Sending Emails and VBA coding
    By lucy1216 in forum Programming
    Replies: 4
    Last Post: 05-20-2013, 05:57 AM
  4. Sending data from form to multiple emails
    By GeorgeB in forum Access
    Replies: 11
    Last Post: 01-09-2012, 04:23 PM
  5. Sending multiple emails
    By Petefured in forum Programming
    Replies: 0
    Last Post: 05-24-2011, 03:40 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