Results 1 to 4 of 4

Sending emails with combination of two module

Threaded View

  1. #1
    Forum Contributor
    Join Date
    11-01-2016
    Location
    Prague
    MS-Off Ver
    2013
    Posts
    121

    Sending emails with combination of two module

    Hi Everyone,

    I have an employee list as attached, with manager data in column "V".

    Macro is sending separate emails to each manager with an attachment. For example, a manager have 5 employee. Macro is copying that 5 row and pasting into a blank excel file and attaching that excel file to an email and sending it to the manager.

    Until here, I am able to do it with below code. It is working.

    But now I have a "Sheet2" which I want to do same thing. Filter the manager's employees in Sheet2 and create a second excel file and attach it into that email as well with previous attachment. Basically It will be one email with two excel attachment. Macro needs to match with managers and excel files.

    I did the same code to Sheet2 as well but I dont know how to combine these two macro and send one email with two attachment ://

    Any idea ?


    Thanks a lot !
    Orhan


    Sub discrepancy()
        Dim Source As Range
        Dim Dest As Workbook
        Dim wb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim OutApp As Outlook.Application
        Dim OutMail As Outlook.MailItem
        Dim header As Range
        Set Source = Nothing
        Dim recepient As String
        Dim Email_Body As String
        On Error Resume Next
        Dim overtimes As Range
    
        Application.ScreenUpdating = True
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        
    ActiveWorkbook.Sheets(1).Activate
            
        lastrow = Sheets(1).UsedRange.Rows.Count
        lastColumn = 22
        With Sheets(1)
            .Range(Cells(2, 1), Cells(lastrow, lastColumn)).Select
            Set overtimes = Selection
            overtimes.Sort Key1:=Range("V2"), Order1:=xlAscending
        End With
        With Sheets(1)
            .Range(Cells(1, 1), Cells(1, lastColumn)).Select
            Set header = Selection.SpecialCells(xlCellTypeVisible)
        End With
        With Sheets(1)
            For i = 2 To lastrow
                If .Cells(i, 22).Value <> .Cells(i - 1, 22).Value Then
                    startrow = .Cells(i, 22).Row
                End If
                If .Cells(i, 22).Value <> .Cells(i + 1, 22).Value Then
                    endrow = .Cells(i, 22).Row
                    .Range(Cells(startrow, 1), Cells(endrow, lastColumn)).Select
                Else
                    GoTo 1
                End If
                recepient = .Cells(i, 22).Value
                Manager = .Cells(i, 22).Value
                Set Source = Selection.SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
            
                If Source Is Nothing Then
                    MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
                    Exit Sub
                End If
            
                With Application
                    .ScreenUpdating = False
                    .EnableEvents = False
                End With
                
                Set wb = ActiveWorkbook
                Set Dest = Workbooks.Add(xlWBATWorksheet)
            
                header.Copy
                With Dest.Sheets(1)
                    .Cells(1, 1).PasteSpecial Paste:=8
                    .Cells(1, 1).PasteSpecial Paste:=xlPasteValues
                    .Cells(1, 1).PasteSpecial Paste:=xlPasteFormats
                    .Cells(1, 1).Select
                    Application.CutCopyMode = False
                End With
                Source.Copy
                With Dest.Sheets(1)
                    .Cells(2, 1).PasteSpecial Paste:=8
                    .Cells(2, 1).PasteSpecial Paste:=xlPasteValues
                    .Cells(2, 1).PasteSpecial Paste:=xlPasteFormats
                    .Cells(2, 1).Select
                    Application.CutCopyMode = False
                End With
            
                TempFilePath = Environ$("temp") & "\"
                TempFileName = "Vacation balance report" & " " & Manager
                emailpath = "C:\Users\celilogl\Desktop\SVK\discrepancy\"
            
                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2016
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            
                Set OutApp = CreateObject("Outlook.Application")
                Set OutMail = OutApp.CreateItem(olMailItem)
                
                Email_Body = "Hello,"
                Email_Body = Email_Body & "<br>" & "<br>" & "Please find attached the vacation days overview for your reports at the end of the last month."
                Email_Body = Email_Body & "<br>" & "<br>" & "Kind Regards,"
                Email_Body = Email_Body & "<br>" & "<br>" & "HR Services"
            
                With Dest
                    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
                    On Error Resume Next
                    With OutMail
                        .To = recepient & "@amazon.com"
                        .CC = ""
                        .BCC = ""
                        .Subject = "Vacation balance report"
                        .BodyFormat = olFormatHTML
                        .HTMLBody = Email_Body
                        .Attachments.Add Dest.FullName
                        '.Send
                        'You can add other files also like this
                        '.Attachments.Add ("C:\test.txt")
                       .SaveAs emailpath & TempFileName & ".msg"
                    End With
                    On Error GoTo 0
                    .Close savechanges:=False
                End With
                Kill TempFilePath & TempFileName & FileExtStr
            
                Set OutMail = Nothing
                Set OutApp = Nothing
            
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
    1    Next i
        End With
    End Sub
    Attached Files Attached Files
    Last edited by orhanceliloglu; 04-17-2018 at 07:30 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Sending emails
    By mshussain in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-02-2016, 03:14 PM
  2. Sending Emails under conditions!
    By JVerwee in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2013, 05:57 AM
  3. [SOLVED] Sending Outlook emails from Excel; Limits to three emails only?
    By BPSJACK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2013, 06:53 AM
  4. Vba for sending emails
    By shagrath in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-26-2011, 10:28 AM
  5. Sending Emails VBA - Specify Who From
    By BenRoylance in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-09-2010, 06:00 AM
  6. Sending emails
    By Sibrulotte in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-26-2010, 10:31 AM
  7. Sending macro emails using excel: Send emails with their passwords.
    By loveisblind in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2009, 03:16 PM

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