Consulting

Results 1 to 2 of 2

Thread: small logic is missing (auto mail)

  1. #1

    small logic is missing (auto mail)

    Hi,

    First of all, Sorry for my bad english.

    With my basic knowledge I created vba code for sending auto mails. But in the code first if condition is not executing. i tried in different ways. but all are went in vain. Pls help me.



    Criteria:
    1) if employee status is "Resigned" ("Status" column mentioned in "Master" sheet) then mail should not go to that person.


    2) if employee status is "Working" and there is no individual attachment for him, then mail should go with remaining all attachments (attachment file path's are mentioned in 'column F' in "Emp Information" Sheet).


    3) if employee status is "Working" and there is individual attachment, then mail should go with remaining all attachments (attachment file paths mentioned in each individual cell in 'Column F' in "Emp Information" sheet) including individual attachment (attachment file path mentioned in each individual cell in 'Column F' in "Emp Information" sheet).

    Example:

    1)Employee's A1,A5,A9,A10,A15 are resigned. So they should not get email.
    2) Employee A3 is Working and there is no file to attach for them (Column F in "Emp information" sheet). then Mail should go with remaining all attachments in column G in "Emp information" Sheet ("Covering letter.txt","1.txt")
    3) for employees A2,A4,A6,A7,A8,A9,A11,A13,A14 , mail should go with all attachments (individual & remaining files)

    I hope criteria's are clear.



    Code is perfect except one error. if there are more than one resigned employees (A9 and A10 employees details are in a row in "Emp information" Sheet. They both are resigned. ) details came one by one, then error occurs.

    i can't understand where the mistake was occurred in my code. Pls help me

    Thank you in advance

    Attached Files Attached Files

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    ?
    Sub vbax_58311_email_based_on_condition()
    
        Dim i As Long, j As Long
        
        Worksheets("Emp Information").Select
    
        If Application.CountA(Columns(7)) > 1 Then
            Range("G2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("G2"), Order1:=xlAscending, Header:=xlYes
        Else
            MsgBox "Please enter file full names to attach to emails to each working employee."
            Exit Sub
        End If
        'to ensure there are file full names and no blank cells in col G. no need to interrupt the code and make the user do this.
        
        With CreateObject("Outlook.Application")
            For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
                If Range("C" & i).Value = "Working" Then
                    With .CreateItem(olMailItem)
                        .To = Range("B" & i).Value
                        .Subject = Range("D" & i).Value
                        .Body = Range("E" & i).Value
                        If Len(Range("F" & i)) > 0 Then .Attachments.Add Range("F" & i).Value
                        For j = 2 To Range("G" & Rows.Count).End(xlUp).Row
                            .Attachments.Add Range("G" & j).Value
                        Next j
                        .Display
                    End With
                End If
            Next i
        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)

Posting Permissions

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