+ Reply to Thread
Results 1 to 9 of 9

Looping Email- Multiple Attachments but I Only Want Selected Attachment

Hybrid View

  1. #1
    Registered User
    Join Date
    04-27-2012
    Location
    New York, USA
    MS-Off Ver
    Excel 2010
    Posts
    63

    Looping Email- Multiple Attachments but I Only Want Selected Attachment

    Hopefully someone will be able to help me figure this one out. I have done some research, and so far no luck.

    I have a function that loops through my spreadsheet and checks the due date for an invoice and whether or not it has been approved and sends out a reminder email to the approver with the invoice as an attachment.

    The first email sent works great, the second attaches the first invoice and the newly selected invoice, the third email attaches the first 2 and the selected one, and so on.

    Does anyone know of a way to clear out attachments after the mail is sent so only the selection attaches to the next email? I appreciate any help I can get!

    Here's my coding:

    Sub Reminder()
    Sheets("INVOICES").Activate
        Dim i As Integer
        Dim Sendmsg
        Dim iMsg As Object
        Dim iConf As Object
        Dim Flds As Variant
        Dim TextBody As String
        Dim ApprovedTextBody As String
        Dim Finfo As String
        Dim FilterIndex As Integer
        Dim FileName As Variant
        Dim Title As String
               
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        Finfo = "All Files (*.*),*.*"
    
        Set iMsg = CreateObject("CDO.Message")
        Set iConf = CreateObject("CDO.Configuration")
    
        iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
    
     'mail server details
        With Flds
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mail_server
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            .Update
        End With
    
        i = 1
        Do While Worksheets("INVOICES").Cells(i, 3) <> ""
        
            If Worksheets("INVOICES").Cells(i, 12).Value <= Date + 7 And Worksheets( _
            "INVOICES").Cells(i, 18) = "" Then
            
                Sendmsg = MsgBox(Prompt:="Are you sure you're ready to send?", _
                Buttons:=vbYesNo, Title:=Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                    Worksheets("INVOICES").Cells(i, 5).Text & " " & Worksheets("INVOICES").Cells(i, 11).Text)
    
                If Sendmsg = vbNo Then
                Exit Sub
                Else
                FileName = Application.GetOpenFilename(Finfo, FilterIndex, Title)
                invoice_file_name = FileName
                
                TextBody = "Hello," & vbNewLine & vbNewLine & _
                    "The attached invoice is still awaiting approval. Kindly advise if this invoice is " & _
                    "approved for payment as soon as you get a chance. " & vbNewLine & vbNewLine & _
                    Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                    Worksheets("INVOICES").Cells(i, 5).Text & vbNewLine & "JSID " & _
                    Worksheets("INVOICES").Cells(i, 3).Text & vbNewLine & _
                    Worksheets("INVOICES").Cells(i, 11).Text & vbNewLine & _
                    Worksheets("INVOICES").Cells(i, 7).Text & " " & Worksheets("INVOICES").Cells(i, 8).Text & _
                    vbNewLine & vbNewLine & "Thank you," & vbNewLine & "cschoyer"
        
                    With iMsg
                    Set .Configuration = iConf
                        .To = Worksheets("INVOICES").Cells(i, 13).Text + "@xyz.com"
                        .CC = ""
                        .BCC = ""
                        .From = """cschoyer"" <[email protected]>"
                        .Subject = "Pending Approval " & Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                                 Worksheets("INVOICES").Cells(i, 5).Text & " JSID " & _
                                 Worksheets("INVOICES").Cells(i, 3).Text
                        .TextBody = TextBody
                        .AddAttachment invoice_file_name
                        .Send
                    End With
                End If
                invoice_file_name = ""
            End If
    
        i = i + 1
        Loop
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    Hello cschoyer,

    The Attachments is a Collection object which is part of the Message object. I have added the line to clear all the attachments after the email is sent. The added line is in bold.
    
                    With iMsg
                    Set .Configuration = iConf
                        .To = Worksheets("INVOICES").Cells(i, 13).Text + "@xyz.com"
                        .CC = ""
                        .BCC = ""
                        .From = """cschoyer"" <[email protected]>"
                        .Subject = "Pending Approval " & Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                                 Worksheets("INVOICES").Cells(i, 5).Text & " JSID " & _
                                 Worksheets("INVOICES").Cells(i, 3).Text
                        .TextBody = TextBody
                        .AddAttachment invoice_file_name
                        .Send
                        .Attchments.DeleteAll
                    End With
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-27-2012
    Location
    New York, USA
    MS-Off Ver
    Excel 2010
    Posts
    63

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    When I run this excel stops working and I get a window that says "Microsoft Excel has stopped working - Windows can check online for a solution to the problem and try to recover your information."

    ...?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    Hello cschoyer ,

    I am not sure why you are getting that message. The code line added is part of the standard Windows CDO library of functions and should not cause Excel to crash. Perhaps it has to do with the timing of sending the attachment before it is deleted. Let's move the the delete statement to the beginning and see it that works.
                    With iMsg
                    .Attchments.DeleteAll
                    Set .Configuration = iConf
                        .To = Worksheets("INVOICES").Cells(i, 13).Text + "@xyz.com"
                        .CC = ""
                        .BCC = ""
                        .From = """cschoyer"" <[email protected]>"
                        .Subject = "Pending Approval " & Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                                 Worksheets("INVOICES").Cells(i, 5).Text & " JSID " & _
                                 Worksheets("INVOICES").Cells(i, 3).Text
                        .TextBody = TextBody
                        .AddAttachment invoice_file_name
                        .Send
                    End With

  5. #5
    Registered User
    Join Date
    04-27-2012
    Location
    New York, USA
    MS-Off Ver
    Excel 2010
    Posts
    63

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    Hm, even with the delete all at the beginning, it still causes excel to crash.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    Hello cschoyer,

    Are any of the emails being sent or does it crash on the first email?

  7. #7
    Registered User
    Join Date
    04-27-2012
    Location
    New York, USA
    MS-Off Ver
    Excel 2010
    Posts
    63

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    it crashes on the first email

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    Hello cschoyer ,

    After looking over your macro, I found a few problems. Since this obviously only part of the code, these may not be problems but simply variables that set elsewhere.

    1. Mail_Server this not set anywhere in the macro. It should be set to name of the mail server you are using, like "www.google.com".
    2. Your port is set to 25 which is fine if the server does not require you to log in.
    3. iConf was incorrectly assigned as CDO.Configuration when it should have been CDO.Message.Configuration.

    Make the needed corrections to 1 and 2 in the code below. Once you have, try running it again.
    
    Sub Reminder()
        
        Dim i As Integer
        Dim Sendmsg
        Dim iMsg As Object
        Dim iConf As Object
        Dim Flds As Variant
        Dim TextBody As String
        Dim ApprovedTextBody As String
        Dim Finfo As String
        Dim FilterIndex As Integer
        Dim FileName As Variant
        Dim Title As String
               
            With Application
                .EnableEvents = False
                .ScreenUpdating = False
            End With
        
            Finfo = "All Files (*.*),*.*"
    
            Set iMsg = CreateObject("CDO.Message")
            Set iConf = iMsg.Configuration
    
            iConf.Load -1    ' CDO Source Defaults
    
          ' mail server details
            With iConf.Fields
                .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mail_server
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                .Update
            End With
    
            i = 1
            Do While Worksheets("INVOICES").Cells(i, 3) <> ""
        
                If Worksheets("INVOICES").Cells(i, 12).Value <= Date + 7 And Worksheets("INVOICES").Cells(i, 18) = "" Then
            
                    Sendmsg = MsgBox(Prompt:="Are you sure you're ready to send?", _
                            Buttons:=vbYesNo, Title:=Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                            Worksheets("INVOICES").Cells(i, 5).Text & " " & Worksheets("INVOICES").Cells(i, 11).Text)
    
                    If Sendmsg = vbNo Then
                        Exit Sub
                    Else
                        FileName = Application.GetOpenFilename(Finfo, FilterIndex, Title)
                        invoice_file_name = FileName
                
                        TextBody = "Hello," & vbNewLine & vbNewLine & _
                                "The attached invoice is still awaiting approval. Kindly advise if this invoice is " & _
                                "approved for payment as soon as you get a chance. " & vbNewLine & vbNewLine & _
                                Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                                Worksheets("INVOICES").Cells(i, 5).Text & vbNewLine & "JSID " & _
                                Worksheets("INVOICES").Cells(i, 3).Text & vbNewLine & _
                                Worksheets("INVOICES").Cells(i, 11).Text & vbNewLine & _
                                Worksheets("INVOICES").Cells(i, 7).Text & " " & Worksheets("INVOICES").Cells(i, 8).Text & _
                                vbNewLine & vbNewLine & "Thank you," & vbNewLine & "cschoyer"
        
                        With iMsg
                            Set .Configuration = iConf
                            .To = Worksheets("INVOICES").Cells(i, 13).Text + "@xyz.com"
                            .CC = ""
                            .BCC = ""
                            .From = """cschoyer"" <[email protected]>"
                            .Subject = "Pending Approval " & Worksheets("INVOICES").Cells(i, 6).Text & " Inv " & _
                                    Worksheets("INVOICES").Cells(i, 5).Text & " JSID " & _
                                    Worksheets("INVOICES").Cells(i, 3).Text
                            .TextBody = TextBody
                            .AddAttachment invoice_file_name
                            .Send
                        End With
                    End If
                    
                    iMsg.Attachments.DeletAll
                    invoice_file_name = ""
                End If
    
                i = i + 1
            Loop
        
            With Application
                .EnableEvents = True
                .ScreenUpdating = True
            End With
    
    End Sub

  9. #9
    Registered User
    Join Date
    04-27-2012
    Location
    New York, USA
    MS-Off Ver
    Excel 2010
    Posts
    63

    Re: Looping Email- Multiple Attachments but I Only Want Selected Attachment

    Your right, this is only part of my code so issues 1 and 2 are taken care of elsewhere.

    Thanks for picking up on error 3- this code works brilliantly, exactly as I wanted it to. Thank you so much for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1