+ Reply to Thread
Results 1 to 12 of 12

Individual Outlook mails with attachments (select from folder)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-16-2020
    Location
    India
    MS-Off Ver
    MS office 365
    Posts
    63

    Individual Outlook mails with attachments (select from folder)

    I have some data in excel where the Gen no (Employee No) and other information are given.
    I have one folder on the Desktop name as Full&Final in path - "C:\Users\Sanjay Kumar\Desktop\F&F".
    In the Full&Final folder i have sub folders based on Gen Nos.
    Example in the Full&Final folder I have 2 different sub folders name as 200298 and 200267.
    For attachments - In the sub folders there are PDFs with the Gen no_ Payslip and Gen no_ IT and Gen no_Name.
    For Example in the sub folder 200298 there are 3 pdfs, 200298_Payslip 200298_IT Payslip 200298_Sanjay
    I want to add only attachments with 200298_Payslip 200298_IT.
    E-mail address and other information are in excel workbook.

    I want to send mail through outlook to all individual though e-mail mention in column "AN"

    for More clarification i have attached screen shots of final version of outlook mail which need to be sent with all details.

    Appreciate if anyone can help help me solve this big problem.

    Thanks
    Attached Files Attached Files
    Last edited by sanjay.k; 10-31-2020 at 04:55 AM. Reason: more clear title

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Individual Outlook mails with attachments (select from folder)

    so you want to loop through the subdirs, pick out the right PDFs, and send them as attachments in individual emails?

  3. #3
    Registered User
    Join Date
    06-16-2020
    Location
    India
    MS-Off Ver
    MS office 365
    Posts
    63

    Re: Individual Outlook mails with attachments (select from folder)

    Dear vba_php,
    Yes . You are correct.
    Last edited by sanjay.k; 10-31-2020 at 11:43 PM. Reason: to specific person reply

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Individual Outlook mails with attachments (select from folder)

    Try

    Option Explicit
    
    
    Sub emailattachpdffile()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim i As Integer
        Dim lr As Integer
        Dim Path As String
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        Application.ScreenUpdating = False
        lr = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row 'For troubleshooting
        
        For i = 2 To Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
            If IsEmpty(Cells(i, "AP").Value) Then 'Check for date
            Path = "C:\Users\Sanjay Kumar\Desktop\F&F\" & Sheet1.Cells(i, "C") & "\"
            With OutMail
                .To = Sheet1.Cells(i, "AN").Value
                .CC = ""
                .Subject = "Full & Final Settlement || " & Sheet1.Cells(i, "B") & " - " & Sheet1.Cells(i, "C")
                .HTMLBody = "Dear " & Cells(i, "D") & "," & Chr(11) & Chr(11) & "Please find the full Final Settlement." & Chr(11) & Chr(11) & "Thanks and Regards" & Chr(11) & Chr(11) & .HTMLBody
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_Payslip.pdf"
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_IT.pdf"
                .Display    'DELETE THIS LINE IF USING SEND
                '.send
            End With
            Set OutMail = Nothing
            Set OutApp = Nothing
            Sheet1.Cells(i, "AP").Value = Date 'Enter date in Column AP
            End If
        Next i
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
    End Sub
    Last edited by maniacb; 10-31-2020 at 06:10 PM.

  5. #5
    Registered User
    Join Date
    06-16-2020
    Location
    India
    MS-Off Ver
    MS office 365
    Posts
    63

    Re: Individual Outlook mails with attachments (select from folder)

    Dear Maniacb,

    only one mail is able to send and gives and not able to send mails from second line. Error highlighted in below line.
    .To = Sheet1.Cells(i, "AN").Value

    Error discreption
    Run time error '91:
    Object Variable or with block variable not set.


    Also there is no need to fill the dates for AP Column. Nothing has to be done in AP column.

  6. #6
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Individual Outlook mails with attachments (select from folder)

    alright, try it now

    Option Explicit
    
    
    Sub emailattachpdffile()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim i As Integer
        Dim lr As Integer
        Dim Path As String
    
        Application.ScreenUpdating = False
        lr = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row 'For troubleshooting
        
        For i = 2 To Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
            'If IsEmpty(Cells(i, "AP").Value) Then 'Check for date
            Path = "C:\Users\Sanjay Kumar\Desktop\F&F\" & Sheet1.Cells(i, "C") & "\"
            With OutMail
                .To = Sheet1.Cells(i, "AN").Value
                .CC = ""
                .Subject = "Full & Final Settlement || " & Sheet1.Cells(i, "B") & " - " & Sheet1.Cells(i, "C")
                .HTMLBody = "Dear " & Cells(i, "D") & "," & Chr(11) & Chr(11) & "Please find the full Final Settlement." & Chr(11) & Chr(11) & "Thanks and Regards" & Chr(11) & Chr(11) & .HTMLBody
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_Payslip.pdf"
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_IT.pdf"
                .Display    'DELETE THIS LINE IF USING SEND
                '.send
            End With
            Set OutMail = Nothing
            Set OutApp = Nothing
            'Sheet1.Cells(i, "AP").Value = Date 'Enter date in Column AP
            'End If
        Next i
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
    End Sub

  7. #7
    Registered User
    Join Date
    06-16-2020
    Location
    India
    MS-Off Ver
    MS office 365
    Posts
    63

    Re: Individual Outlook mails with attachments (select from folder)

    Dear maniacb,
    Now it is working.
    but when the file is missing then it is showing an error and not moved to next line.
    Can it be possible that if mail are sent then in AQ column it comes as "mail successfully sent". and moved to next line
    if not able to sent then "file name - missing mail not sent" and moved to next line.

  8. #8
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Individual Outlook mails with attachments (select from folder)

    Give this code a whirl:

    Sub emailattachpdffile()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim i As Integer
        Dim lr As Integer
        Dim Path As String
    
        Application.ScreenUpdating = False
        lr = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
        On Error Resume Next
        For i = 2 To Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
            'If IsEmpty(Cells(i, "AP").Value) Then 'Check for date
            Path = "C:\Users\Sanjay Kumar\Desktop\F&F\" & Sheet1.Cells(i, "C") & "\"
            With OutMail
                .To = Sheet1.Cells(i, "AN").Value
                .CC = ""
                .Subject = "Full & Final Settlement || " & Sheet1.Cells(i, "B") & " - " & Sheet1.Cells(i, "C")
                .HTMLBody = "Dear " & Cells(i, "D") & "," & Chr(11) & Chr(11) & "Please find the full Final Settlement." & Chr(11) & Chr(11) & "Thanks and Regards" & Chr(11) & Chr(11) & .HTMLBody
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_Payslip.pdf"
                If Err.Number <> 0 And Err.Number = -2147024893 Then
                    'MsgBox "Missing Path at row # " & i
                    Sheet1.Cells(i, "AQ").Value = "File has NOT been sent"
                    On Error GoTo -1
                    GoTo skip
                End If
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_IT.pdf"
                If Err.Number <> 0 And Err.Number = -2147024893 Then
                    'MsgBox "Missing Path at row # " & i
                    Sheet1.Cells(i, "AQ").Value = "File has NOT been sent"
                    On Error GoTo -1
                    GoTo skip
                End If
                .Display    'DELETE THIS LINE IF USING SEND
                '.send
            End With
            Sheet1.Cells(i, "AQ").Value = "File has been sent succesfully"
    skip:
            Set OutMail = Nothing
            Set OutApp = Nothing
            'End If
        Next i
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
    
    End Sub
    Last edited by maniacb; 11-01-2020 at 02:23 AM.

  9. #9
    Registered User
    Join Date
    06-16-2020
    Location
    India
    MS-Off Ver
    MS office 365
    Posts
    63

    Re: Individual Outlook mails with attachments (select from folder)

    Dear maniacb,
    It is always showing in AQ "File has NOT been sent" and no mails are able to sent (even when the files are correct in the folder)

  10. #10
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Individual Outlook mails with attachments (select from folder)

    Try now. I was trapping the wrong error and had to recreate your environment to find the right error code.

    Sub emailattachpdffile()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim i As Integer
        Dim lr As Integer
        Dim Path As String
    
        Application.ScreenUpdating = False
        lr = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
        On Error Resume Next
        For i = 2 To Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
            'If IsEmpty(Cells(i, "AP").Value) Then 'Check for date
            Path = "C:\Users\Sanjay Kumar\Desktop\F&F\" & Sheet1.Cells(i, "C") & "\"
            With OutMail
                .To = Sheet1.Cells(i, "AN").Value
                .CC = ""
                .Subject = "Full & Final Settlement || " & Sheet1.Cells(i, "B") & " - " & Sheet1.Cells(i, "C")
                .HTMLBody = "Dear " & Cells(i, "D") & "," & Chr(11) & Chr(11) & "Please find the full Final Settlement." & Chr(11) & Chr(11) & "Thanks and Regards" & Chr(11) & Chr(11) & .HTMLBody
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_Payslip.pdf"
                If Err.Number <> 0 And Err.Number = -2147024894 Then
                    'MsgBox "Missing Payslip File at row # " & i
                    Sheet1.Cells(i, "AQ").Value = "File has NOT been sent"
                    On Error GoTo -1
                    GoTo skip
                End If
                .Attachments.Add Path & Sheet1.Cells(i, "C") & "_IT.pdf"
                If Err.Number <> 0 And Err.Number = -2147024894 Then
                    'MsgBox "Missing IT File at row # " & i
                    Sheet1.Cells(i, "AQ").Value = "File has NOT been sent"
                    On Error GoTo -1
                    GoTo skip
                End If
                .Display    'DELETE THIS LINE IF USING SEND
                '.send
            End With
            Sheet1.Cells(i, "AQ").Value = "File has been sent successfully"
    skip:
            Set OutMail = Nothing
            Set OutApp = Nothing
            'End If
        Next i
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
    Exit Sub
    
    End Sub

  11. #11
    Registered User
    Join Date
    06-16-2020
    Location
    India
    MS-Off Ver
    MS office 365
    Posts
    63

    Re: Individual Outlook mails with attachments (select from folder)

    Dear Maniacb,
    you are really very helpful and awesome.
    The code is working fine now .

  12. #12
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Individual Outlook mails with attachments (select from folder)

    If that takes care of your original question, please select*Thread Tools*from the menu link above and mark this thread as SOLVED.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Generating PDF Payslips form Payroll in Excel and sending to Individual e-mails in Outlook
    By Akintomiwa in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 08-27-2021, 08:47 PM
  2. Replies: 0
    Last Post: 12-08-2019, 05:30 PM
  3. [SOLVED] Send multiple e-mails with attachments via excel file and outlook
    By makados in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-03-2019, 03:34 AM
  4. Send mails with attachments using a macro
    By Warmerfare in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-27-2017, 08:13 AM
  5. Excel containing macro to send bulk mails from outlook with multiple attachments
    By amandeep08 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2013, 02:19 PM
  6. Individual Attachments (Outlook 2010)
    By MooKwia in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-18-2013, 12:38 AM
  7. [SOLVED] Excel Macro to download attachments from multiple sub folders of outlook
    By abhay_547 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-31-2010, 09:32 AM

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