Home » excel » excel vba – Attach multiple files to Outlook email from a filtered list and loop

excel vba – Attach multiple files to Outlook email from a filtered list and loop

Posted by: admin May 14, 2020 Leave a comment


I have a list of customers with their invoice data (one customer may have one or more than one rows of data). I have assembled a macro script from multiple codes to filter out the customer (basis on email address) and send them a dunning letter with their account statement.

The code is working fine with email creation, except I am not able to attach their invoice copies listed in column 2 (In TempoWB workbook).

I think the problem is with Loop The code is jumping from Do while directly to .HTMLBody.It’s skipping the previous codes to search and attach files. How can I fix it?

Here is the Zip file with all required data and files. In case you want to give it a try. Just copy the ‘Renamed’ invoice folder to C:\Invoices.

(customer names and other data has been altered for compliance reason)

    Option Explicit

    Sub Dunning_3_Populate_Emails_TempWB()

     Application.ScreenUpdating = False

    'This code populates emails to outlook as per the Credit analysts.

        Dim OutApp As Object
        Dim OutMail As Object
        Dim rng As Range
        Dim Ash As Worksheet
        Dim Cws As Worksheet
        Dim Rcount As Long
        Dim Rnum As Long
        Dim name_rg As Range
        Dim name As String
        Dim Subj As String
        Dim irow As Integer
        Dim dpath As String
        Dim pfile As String
        Dim strbody As String
        Dim TempoWB As Workbook

     'Folder location for Invoice copies

        dpath = "C:\Invoices\Renamed"

     'Column number to pick the invoices
        irow = 2

        Set OutApp = CreateObject("Outlook.Application")

         name = Ash.Cells(name_rg.Row, 16)
         Subj = Ash.Cells(name_rg.Row, 15)
         name = "email not found in Ash"
        End If
    'This portion has codes to filter the required data based on the unique email address


     'Create a new workbook with selected/ filtered data
        Set TempoWB = Workbooks.Add(1)
        With TempoWB.Sheets(1)
             Application.CutCopyMode = False
             On Error Resume Next
            .DrawingObjects.Visible = True
             Selection.Delete Shift:=xlToLeft
             On Error GoTo 0

     'Location to save the temporary workbook
             Application.DisplayAlerts = False
             TempoWB.SaveAs Filename:="C:\Invoices\TempoWB.xlsx"
        End With

     'E-mail body for the dunning letters

        strbody = "Hello " & name & "," & "<br>" & "<br>" & _

                  "<b>Below is the summary of your account and attached are the invoices:</b>" & "<br>" & "<br>"

        On Error GoTo Cleanup

        On Error Resume Next

    With OutMail
        .To = Cws.Cells(Rnum, 1).Value
        .Subject = subj

For irow = 2 To Lastrow

        .Attachments.Add ("C:\Dunning Temp\" & Cells(irow, 2).Value & ".pdf")


        .HTMLBody = strbody & RangetoHTML(rng) & .HTMLBody
    End With

    On Error GoTo 0

    Set OutMail = Nothing
    End If

 'Close TempoWB
    Application.DisplayAlerts = False
    Workbooks("TempoWB.xlsx").Close SaveChanges:=False
    On Error Resume Next

 'Close AutoFilter
    Ash.AutoFilterMode = False

    Next Rnum

    End If

    End Sub
How to&Answers:

My initial suspicion of the row counter was just flat wrong. The problem turned out to be several small errors that simply caused the code to look for the attachment in places it could never find it.

Two things you should know:

1) The code currently in your question didn’t feel right so I tossed it and went with the version you originally posted.

2) You need to update the path/directory strings and clear some comment blocks I’ve made. Nothing too difficult.

And lastly, the nested while loops are the reason you started getting stuck in a loop. Reducing your code to illustrate the point, it looked something ike this:

Both conditions will almost always be met so you get stuck on the inside loop if the cell is empty and you get stuck on the outside loop if the cell is populated.