Consulting

Results 1 to 11 of 11

Thread: VBA for sending Bulk Email with Unique Attachments in XLS to Unique Receipient

  1. #1

    Post VBA for sending Bulk Email with Unique Attachments in XLS to Unique Receipient

    Dear Friends

    Its a VBA made by one of our friend for sending bulk mails to unique mail id with unique attachments, but the same in not functioning as designed as its not picking up the attachment from defined range, can anyone suggest where the problem lies.



    Public Sub Send_multiple_email_with_attachements()
    Dim objOutlook As Object
    Dim objMail As Object
    Dim rngTo As Range
    Dim rngSubject As Range
    Dim rngBody As Range
    Dim rngAttach As Range
    
    On Error Resume Next
    
    With ActiveSheet
    lastrowTO = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    With ActiveSheet
    lastrowCC = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With
    
    With ActiveSheet
    lastrowBCC = .Cells(.Rows.Count, "C").End(xlUp).Row
    End With
    
    x = WorksheetFunction.Max(lastrowTO, lastrowCC, lastrowBCC)
    
    For t = 3 To x
    
    If ActiveSheet.Range("K" & t) = "Yes" Or ActiveSheet.Range("K" & t) = "yes" Then
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)
    
    With ActiveSheet
    Set rngTo = ActiveSheet.Range("A" & t)
    Set rngCC = ActiveSheet.Range("B" & t)
    Set rngBCC = ActiveSheet.Range("C" & t)
    Set rngSubject = ActiveSheet.Range("D" & t)
    Set rngBody = ActiveSheet.Range("E" & t)
    Set rngAttach = ActiveSheet.Range("G" & t)
    Set rngVoting = ActiveSheet.Range("J" & t)
    End With
    
    With objMail
    .VotingOptions = rngVoting.Value
    .To = rngTo.Value
    .cc = rngCC.Value
    .bcc = rngBCC.Value
    .Subject = rngSubject.Value
    .Body = rngBody.Value
    .Attachments.Add rngAttach.Value
    .send
    End With
    
    Set objOutlook = Nothing
    Set objMail = Nothing
    Set rngTo = Nothing
    Set rngSubject = Nothing
    Set rngBody = Nothing
    Set rngAttach = Nothing
    
    End If
    
    Next t
    End Sub
    moreover can anyone suggest modifications if any to incorporate Mail Body in the format as below


    Dear Sir,

    Attached please find Outstanding as on 31.3.2017. Kindly Review and settle overdue Invoices.

    Thanks & Regards
    Md Asif Iqbal
    Last edited by mdmackillop; 04-23-2017 at 03:52 AM. Reason: Code tags added

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    worked for me
    use .Display to test the procedure.
    use .Send to send emails.

    if you dont specify the sheet of a cell, it refers to the cell in activesheet. using ActiveSheet.Range("A1") is redundant. Just Range("A1")

    Sub vbax_59255_mass_emailing()
    
        Dim t As Long, LastRow As Long
        Dim mBody As String
        
        LastRow = Range("A:C").Find("*", , , , xlByRows, xlPrevious).Row
        
        mBody = "Dear Sir," & vbLf & vbLf
        mBody = mBody & "Attached please find Outstanding as on 31.3.2017. Kindly Review and settle overdue Invoices." & vbLf & vbLf
        mBody = mBody & "Thanks & Regards" & vbLf
        mBody = mBody & "Md Asif Iqbal"
    
        With CreateObject("Outlook.Application")
            For t = 3 To LastRow
                If UCase(Range("K" & t)) = "YES" Then
                    With .CreateItem(0)
                        .VotingOptions = Range("J" & t).Value
                        .To = Range("A" & t).Value
                        .CC = Range("B" & t).Value
                        .BCC = Range("C" & t).Value
                        .Subject = Range("D" & t).Value
                        .Body = mBody
                        .Attachments.Add Range("G" & t).Value
                        .Display 
                        '.Send
                    End With
                End If
            Next t
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Dear Mancubus

    I incorporated your you and its working fine except the attachment part, have attached the code kindly check every thing is in order

    Public Sub Send_multiple_email_with_attachements()
        Dim objOutlook As Object
        Dim objMail As Object
        Dim rngTo As Range
        Dim rngSubject As Range
        Dim rngAttach As Range
        Dim t As Long, Lastrow As Long
        Dim mBody As String
    
    
         
        On Error Resume Next
        
        mBody = mBody & vbLf & vbLf
        mBody = mBody & "Attached please find Outstanding as on 31.3.2017. Kindly Review and settle overdue Invoices." & vbLf & vbLf
        mBody = mBody & "Thanks & Regards" & vbLf
        mBody = mBody & "Md Asif Iqbal"
         
         
         
        With ActiveSheet
            lastrowTO = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
         
        With ActiveSheet
            lastrowCC = .Cells(.Rows.Count, "B").End(xlUp).Row
        End With
         
        With ActiveSheet
            lastrowBCC = .Cells(.Rows.Count, "C").End(xlUp).Row
        End With
         
        x = WorksheetFunction.Max(lastrowTO, lastrowCC, lastrowBCC)
         
        For t = 3 To x
             
            If ActiveSheet.Range("K" & t) = "Yes" Or ActiveSheet.Range("K" & t) = "yes" Then
                 
                Set objOutlook = CreateObject("Outlook.Application")
                Set objMail = objOutlook.CreateItem(0)
                 
                With ActiveSheet
                    Set rngTo = ActiveSheet.Range("A" & t)
                    Set rngCC = ActiveSheet.Range("B" & t)
                    Set rngBCC = ActiveSheet.Range("C" & t)
                    Set rngSubject = ActiveSheet.Range("D" & t)
                    Set rngBody = ActiveSheet.Range("E" & t)
                                  
                    Set rngAttach = ActiveSheet.Range("G" & t)
                    Set rngVoting = ActiveSheet.Range("J" & t)
                End With
                 
                With objMail
                    .VotingOptions = rngVoting.Value
                    .To = rngTo.Value
                    .cc = rngCC.Value
                    .bcc = rngBCC.Value
                    .Subject = rngSubject.Value
                    .Body = rngBody & mBody
                    .Attachments.Add rngAttach.Value
                    .display
                    .send
                End With
                 
                Set objOutlook = Nothing
                Set objMail = Nothing
                Set rngTo = Nothing
                Set rngSubject = Nothing
                Set rngBody = Nothing
                Set rngAttach = Nothing
                 
            End If
             
        Next t
    End Sub

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    @mda
    what i posted is a short for all your code.
    you dont need to dim hundreds of variables for objects, methods, etc and assign values to.

    post a sample cell value from which you take attachment names. or, better, post your workbook.
    and dont forget to wrap your code with code tags.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Dear Mancubus

    thanks for the code provided its working fine, but at time the code doesn't not run I don't know why, I'm trying to figure it out may be I'm making changes to the mail body. I just need a bit of tweaking to the code in the mail body the ones I have highlighted in RED should pick up data from the range in Column F, H & I

    mBody = "Dear Sir," & vbLf & vbLf
    mBody = mBody & "Attached please find Outstanding as on 31.3.2017. Kindly Review and settle overdue Invoices." & vbLf & vbLf
    mBody = mBody & "Thanks & Regards" & vbLf
    mBody = mBody & "Md Asif Iqbal

    Thanks & Regards

    Md Asif Iqbal

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Why did you break up with your friend ? Why don't you ask him/her ?

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    no. the red bits dont affect the code. there is a missing double quotes mark after Iqbal but i think it's a typo.


    post your workbook. you may alter the original data. i just need to see the what type of values you have in cells.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Quote Originally Posted by snb View Post
    Why did you break up with your friend ? Why don't you ask him/her ?
    spiritual humor.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    Dear Mancubus

    Attached please find the file on which I'm applying the code, what I meant by Highlighting certain characters in red was that I wanted the field to pick up data from the Range in the xlsx file from columns E, H & I which will be different in individual cases.

    Regards

    Md Asif Iqbal
    Attached Files Attached Files

  10. #10
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Works as expected.
    Creates e-mails for the rows if their column K value is YES.

    i modified the code to take the greeting/salutation part from column E.
    so instead of Dear Sir, mail body starts with Dear Arindom, Dear Nilay, etc.

    just make sure columns A, B, C contain valid e-mail addresses and column G valid full file name.
    if the name of the file to attach is C:\Users\IqbalM\Desktop\BIN\AR\A.D. REGENCY EXPORTS PVT. LTD.xlsx, a file named A.D. REGENCY EXPORTS PVT. LTD.xlsx must exist in the folder C:\Users\IqbalM\Desktop\BIN\AR\.

    and this codes assumes active sheet contains all the mailing info.
    so run the code if Sheet1 (taking into account the uploaded file) is the activesheet.

    Sub vbax_59255_mass_emailing()
         
        Dim t As Long, LastRow As Long
        Dim mBody As String
         
        LastRow = Range("A:C").Find("*", , , , xlByRows, xlPrevious).Row
         
        mBody = vbLf & vbLf
        mBody = mBody & "Attached please find Outstanding as on 31.3.2017. Kindly Review and settle overdue Invoices." & vbLf & vbLf
        mBody = mBody & "Thanks & Regards" & vbLf
        mBody = mBody & "Md Asif Iqbal"
         
        With CreateObject("Outlook.Application")
            For t = 3 To LastRow
                If UCase(Range("K" & t)) = "YES" Then
                    With .CreateItem(0)
                        .VotingOptions = Range("J" & t).Value
                        .To = Range("A" & t).Value
                        .CC = Range("B" & t).Value
                        .BCC = Range("C" & t).Value
                        .Subject = Range("D" & t).Value
                        .Body = Range("E" & t).Value & mBody
                        .Attachments.Add Range("G" & t).Value
                        .Display
                         '.Send
                    End With
                End If
            Next t
        End With
         
    End Sub
    test your code with
    when testing, make sure there is at least one Yes in K3:K8
    Sub vbax_59255_mass_emailing_test()
         
        Dim t As Long, LastRow As Long
        Dim mBody As String
         
        LastRow = Range("A:C").Find("*", , , , xlByRows, xlPrevious).Row
         
        mBody = vbLf & vbLf
        mBody = mBody & "Attached please find Outstanding as on 31.3.2017. Kindly Review and settle overdue Invoices." & vbLf & vbLf
        mBody = mBody & "Thanks & Regards" & vbLf
        mBody = mBody & "Md Asif Iqbal"
         
        With CreateObject("Outlook.Application")
            For t = 3 To 8 'loop thru 3-8 rows only
                If UCase(Range("K" & t)) = "YES" Then
                    With .CreateItem(0)
                        .VotingOptions = Range("J" & t).Value
                        .To = Range("A" & t).Value
                        .CC = Range("B" & t).Value
                        .BCC = Range("C" & t).Value
                        .Subject = Range("D" & t).Value
                        .Body = Range("E" & t).Value & mBody
                        .Attachments.Add Range("G" & t).Value
                        .Display 'just display do not send
                    End With
                End If
            Next t
        End With
         
    End Sub
    PS: i have nothing to add regarding this thread. so i can't reply any further questions like "does not work", "works but not as expected", etc.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You data looks hard to maintain. I'd suggest something like the attached to build your email list from basic data which also checks you have a valid attachment.
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Tags for this Thread

Posting Permissions

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