Results 1 to 7 of 7
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Sending out 2 emails with different attachments

    How can I do this with 1 click like sending a single email? I have searched the interent and here, but usually get multiple attachments in 1 email.



    Code:
    Private Sub Send_Open_Click()
     On Error GoTo ErrorMsgs
     Dim objOutlook As Outlook.Application
     Dim objOutlookMsg As Outlook.MailItem
     Dim objOutlookAttach As Outlook.Attachment
     Dim objOutlookRecip As Outlook.Recipient
     Dim strBody, strAddresses, strSubject As String
     Dim CRNUM As Variant
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    CRNUM = DLookup("[CR_Numbers]", "[Open_CRs]", "CR_Numbers")
    If IsNull(CRNUM) Then
    With objOutlookMsg
       .Subject = "There are no Open CR's - " & Format(Date + 1, "dd mmm yyyy")
       .Body = "There are no Change Request actions for tomorrows CMB." & vbCrLf & vbCrLf  & _"
        & V/R" & vbCrLf & vbCrLf & "Name" & vbCrLf & vbCrLf & "Organization" & vbCrLf & "Division" & vbCrLf & "Street" & vbCrLf & "Zip" _&
        vbCrLf & "Phone" & vbCrLf & "Email"
       .Display
        DoCmd.Close acReport, "Open by Level"
    Exit Sub
    End With
    Else
    End If
    With objOutlookMsg
        .Subject = "Tomorrow's CR's - " & Format(Date + 1, "dd mmm yyyy")
        .Body = "For your use." & vbCrLf & vbCrLf & "V/R" & vbCrLf & vbCrLf & "Name" & vbCrLf & vbCrLf & "Organization" & vbCrLf & "Division" & vbCrLf & "Street" & vbCrLf & "Zip" _
        & vbCrLf & "Phone" & vbCrLf & "Email"
        DoCmd.OutputTo 3, "Open by Level", acFormatPDF, "C:\Temp\Open Changes - " & Format(Date + 1, "dd mmm yyyy") & ".pdf", , 0
        .Attachments.Add ("C:\Temp\Open Changes - " & Format(Date + 1, "dd mmm yyyy") & ".pdf")
       .Display
      Kill "C:\Temp\Open Changes - " & Format(Date + 1, "dd mmm yyyy") & ".pdf"
        DoCmd.Close acReport, "Open by Level"
      End With
     
    With objOutlookMsg
         .Subject = "Today's AORB/ERB/CCB outcome - " & Format(Date, "dd mmm yyyy")
         .Body = "Today's AORB/ERB/CCB outcome." & vbCrLf & vbCrLf & strMsg & _
          & "V/R" & vbCrLf & vbCrLf & "Name" & vbCrLf & vbCrLf & "Organization" & vbCrLf & "Division" & vbCrLf & "Street" & vbCrLf & "Zip" _
          & vbCrLf & "Phone" & vbCrLf & "Email"
        DoCmd.OutputTo 3, "Weekly SITREP V", acFormatXLS, "C:\Temp\Summation - " & Format(Date, "dd mmm yyyy") & ".xls", , 0
        DoCmd.Close acForm, "Bi-Summation"
       .Attachments.Add ("C:\Temp\Summation - " & Format(Date, "dd mmm yyyy") & ".xls")
       .To = "HB Rollup"
       .Display
      Kill "C:\Temp\Summation - " & Format(Date, "dd mmm yyyy") & ".xls"
      DoCmd.Close acReport, "Weekly SITREP V"
      End With
    Set objOutlookMsg = Nothing
     Set objOutlook = Nothing
     Set objOutlookAttach = Nothing
      Exit Sub
    ErrorMsgs:
     If Err.Number = "287" Then
     MsgBox "You clicked No to the Outlook security warning. " & _
     "Rerun the procedure and click Yes to access e-mail " & _
     "addresses to send your message."
     Else
     MsgBox Err.Number & " " & Err.Description
    End If
    End With
    End Sub
    This gives me 1 email (The second) with both attachments.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Not understanding the issue. If you want to send a second email then just incorporate code to do that.

    Why do you show the code split up? Where is the second email code positioned in the Sub?

    Step debug. Review link at bottom of my post for debugging guidelines.
    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
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    June, the way I have it set up, I am sending an email with attachment if there is information in the records, otherwise it will send out the email with a statement saying no new issues. I also want to send out another email to a different group with another attachment. The way the code is run now I can see the first email being developed, then it will change automatically to the second email. The second email is position in the start of the "With objOutlookMsg."

    I will edit the first post to show the combination I have. What I am looking for is a code to show the end of the email, and start a new email process and not combine them. Make the 2 emails a complete separate process.

    BLUF - I want to have 2 emails defined as separate entities with their own attachments.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The .Body strings have wrong syntax in the line continuation. You should be getting a compile error.
    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.

  5. #5
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    June,
    Its just my editing mistakes when I take out the signature block, I think. Otherwise I do not get any compilation errors. I am wanting to create 2 seperate emails. With the below code, it creates the first email, then changes it to the second email. I need it to create the second email in a different message.

    Code:
    PPrivate Sub Send_Daily_Click()
      On Error GoTo ErrorMsgs
      Dim rs As DAO.Recordset
      Dim objOutlook As Outlook.Application
      Dim objOutlookMsg As Outlook.MailItem
      Dim objOutlookAttach As Outlook.Attachment
      Dim strBody, strAddresses, strSubject, strMsg As String
      Dim CRNUM As Variant
     Set rs = CurrentDb.OpenRecordset("SELECT Status,CR_Numbers,[Change Requested]FROM Daily_Actions_Email ORDER BY Status,CR_Number ASC")
     Set objOutlook = CreateObject("Outlook.Application")
     Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
     CRNUM = DLookup("[CR_Numbers]", "[Daily_Actions_Email]", "CR_Numbers")
     If IsNull(CRNUM) Then
     With objOutlookMsg
           .Subject = "There were no actioned CR's - " & Format(Date + 1, "dd mmm yyyy")
         .Body = "There were no actioned Change Requests today." & vbCrLf & vbCrLf & "V/R" & vbCrLf & vbCrLf & "Name" & vbCrLf & vbCrLf & "Command" & vbCrLf & "Division" & vbCrLf & "Address" & vbCrLf & "Zip" & vbCrLf & "Phone" & vbCrLf & "Email"
         .To = "TEWG CCB Results"
        .Display
         DoCmd.Close acReport, "Daily Actions"
     Exit Sub
     End With
     Else
     End If
     rs.MoveFirst
     While Not rs.EOF
      strMsg = strMsg & rs!Status & vbCrLf & Chr(9) & "CR  " & rs!CR_Numbers & " - " & rs![Change Requested] & vbCrLf
      rs.MoveNext
     Wend
      rs.Close
     With objOutlookMsg
        .Subject = "Today's AORB/ERB/CCB outcome - " & Format(Date, "dd mmm yyyy")
        .Body = "Today's AORB/ERB/CCB outcome." & vbCrLf & vbCrLf & strMsg & vbCrLf & vbCrLf & "V/R" & vbCrLf & vbCrLf & "Name" & vbCrLf & vbCrLf & "Command" & vbCrLf & "Division" & vbCrLf & " Address" & vbCrLf & "Zip" & vbCrLf & "Phone" & vbCrLf & "Email"
         DoCmd.OutputTo 3, "Daily Actions", acFormatPDF, "C:\Temp\Daily Actions - " & Format(Date, "dd mmm yyyy") & ".pdf", , 0
         .Attachments.Add ("C:\Temp\Daily Actions - " & Format(Date, "dd mmm yyyy") & ".pdf")
         .To = "TEWG CCB Results"
         .Display
         DoCmd.Close acReport, "Daily Actions"
         Kill "C:\Temp\Daily Actions - " & Format(Date, "dd mmm yyyy") & ".pdf"
      End With
     With objOutlookMsg
          .Subject = "Today's AORB/ERB/CCB outcome - " & Format(Date, "dd mmm yyyy")
          .Body = "Today's AORB/ERB/CCB outcome." & vbCrLf & vbCrLf & strMsg & _
                vbCrLf & vbCrLf & "V/R" & vbCrLf & vbCrLf & "Name" & vbCrLf & vbCrLf & "Command" & vbCrLf & "Division" & vbCrLf & "Address" & vbCrLf & "Zip" & vbCrLf & "Phone" & vbCrLf & "Email"
         DoCmd.OutputTo 3, "Weekly SITREP V", acFormatXLS, "C:\Temp\Summation - " & Format(Date, "dd mmm yyyy") & ".xls", , 0
         DoCmd.Close acForm, "Bi-Summation"
        .Attachments.Add ("C:\Temp\Summation - " & Format(Date, "dd mmm yyyy") & ".xls")
        .To = "HB Rollup"
        .Display
       Kill "C:\Temp\Summation - " & Format(Date, "dd mmm yyyy") & ".xls"
       DoCmd.Close acReport, "Weekly SITREP V"
     End With
      Set objOutlookMsg = Nothing
      Set objOutlook = Nothing
      Set objOutlookAttach = Nothing
      Exit Sub
    ErrorMsgs:
      If Err.Number = "287" Then
      MsgBox "You clicked No to the Outlook security warning. " & "Rerun the procedure and click Yes to access e-mail " & "addresses to send your message."
      Else
      MsgBox Err.Number & " " & Err.Description
      End If
      End Sub

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Problem is there is only one Outlook message object and since you only display it (not send) when the second email code runs it is modifying the object.

    So either send instead of display OR declare and set two objects.

    Dim objOutlookMsg1 As Outlook.MailItem
    Dim objOutlookMsg2 As Outlook.MailItem
    Set objOutlookMsg1 = objOutlook.CreateItem(olMailItem)
    Set objOutlookMsg2 = objOutlook.CreateItem(olMailItem)

    With objOutlookMsg1
    ...

    With objOutlookMsg2
    ...
    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.

  7. #7
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    June,
    Thanks, now I can send multiple emails. Much appreciated.

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

Similar Threads

  1. Sending emails without interruptions
    By George in forum Access
    Replies: 14
    Last Post: 03-13-2015, 11:15 PM
  2. sending multiple emails wth vba
    By baronqueefington in forum Programming
    Replies: 2
    Last Post: 02-09-2015, 02:49 PM
  3. Sending Emails and VBA coding
    By lucy1216 in forum Programming
    Replies: 4
    Last Post: 05-20-2013, 05:57 AM
  4. Attachments to Emails
    By MrChips in forum Access
    Replies: 8
    Last Post: 09-01-2012, 11:17 AM
  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