+ Reply to Thread
Results 1 to 3 of 3

Sheet not being attached in Outlook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,768

    Sheet not being attached in Outlook

    I have a sheet called "BTR Intercompany" .In Col A , I have several text items which includes text "Variance". Where there is a value in Col D in same row as "Variance" in Col A that is not zero or absolute value >=0.01, then attach sheet. My Code below only attaches the sheet if the first value in Col D in same row as variance in Col A <> 0 and ABS >=0.01. If the first Value is zero in Col D in same row as "Variance" in Col A , but other values in Col D <> zero or ABS >=0.01 where there is "Variance" in Col A in same row , then sheet is not being attached


    Kindly test & amend my code


    Sub Email_BTR_Intercompany()
    
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("BTR Intercompany")
        
        Dim varCell As Range
        Dim attachSheet As Boolean
        attachSheet = False
        
        For Each varCell In ws.Range("A:A").Find("Variance", LookIn:=xlValues, LookAt:=xlWhole)
            If Abs(varCell.Offset(0, 3).Value) >= 0.01 Then
                attachSheet = True
                Exit For
            End If
        Next varCell
        
        If Not attachSheet Then
            Exit Sub
        End If
        
        Dim rng As Range
        Dim isFirstVariance As Boolean
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim Destwb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim OutApp As Object
        Dim OutMail As Object
        Dim Stringbody As String
        
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.Calculation = xlCalculationManual
        
        ' Copy the ActiveSheet to a new workbook
        ws.Copy
        Set Destwb = ActiveWorkbook
        
        ' Determine the Excel version and file extension/format
        With Destwb
            If Val(Application.Version) < 12 Then
                ' You use Excel 97-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                ' You use Excel 2007-2013
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End With
        
        ' Change all cells in the worksheet to values if you want
        With Destwb.Sheets(1).UsedRange
            .Value = .Value
        End With
        
        Application.CutCopyMode = False
        
        ' Save the new workbook/Mail it/Delete it
        TempFilePath = Environ$("temp") & "\"
        TempFileName = ws.Range("A2") & " Intercompany Variance " & Format(Now, "dd-mmm-yy h-mm-ss")
        
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        With Destwb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ws.Range("L1:L1")
                .CC = ""
                .BCC = ""
                .Subject = "Variance Report: " & ws.Range("A2") & " Intercompany"
                Stringbody = "Hi " & ws.Range("K1").Value & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Attached, please find the Intercompany Variance report for " & ws.Range("A2") & "." & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Please advise once corrected." & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Regards" & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Howard" ' Replace with your name or signature
    
                .Body = Stringbody
                .Attachments.Add Destwb.FullName
                .Display ' Use .Send to send automatically or .Display to check email before sending
            End With
            On Error GoTo 0
            .Close SaveChanges:=False
        End With
    
        ' Delete the file you have sent (optional)
        ' Kill TempFilePath & TempFileName & FileExtStr
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
    
    End Sub
    Last edited by Howardc1001; 08-17-2023 at 01:45 AM.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,477

    Re: Shwet not being attached i

    For Each varCell In ws.Range("A:A").Find("Variance", LookIn:=xlValues, LookAt:=xlWhole)
            If Abs(varCell.Offset(0, 3).Value) >= 0.01 Then
                attachSheet = True    
                Exit For
            End If
        Next varCell
        
        If Not attachSheet Then
            Exit Sub
        End If
    change to

    For Each varCell In ws.Range("A:A").Find("Variance", LookIn:=xlValues, LookAt:=xlWhole)
            If Abs(varCell.Offset(0, 3).Value) >= 0.01 Then
                    
                **** Add your main code here as you only loop through this once with your current code ******
    End If
        Next varCell
        
        If Not attachSheet Then  **** REDUNDANT
            Exit Sub
        End If
    Last edited by JohnTopley; 08-17-2023 at 01:27 AM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,768

    Re: Sheet not being attached in Outlook

    Thanks John for your input. I have amended my code as follows and it works perfectly:


     Sub Email_BTR_Intercompany()
    
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("BTR Intercompany")
        
        Dim lastRow As Long
        lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        
        Dim attachSheet As Boolean
        attachSheet = False
        
        For i = 2 To lastRow ' Assuming data starts from row 2
            
            If InStr(1, ws.Cells(i, 1).Value, "Variance", vbTextCompare) > 0 Then
                If Abs(ws.Cells(i, 4).Value) >= 0.01 Then
                    attachSheet = True
                    Exit For
                End If
            End If
            
        Next i
        
        If Not attachSheet Then
            Exit Sub
        End If
        
        Dim rng As Range
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Destwb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim OutApp As Object
        Dim OutMail As Object
        Dim Stringbody As String
        
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.Calculation = xlCalculationManual
        
        ' Copy the ActiveSheet to a new workbook
        ws.Copy
        Set Destwb = ActiveWorkbook
        
        ' Determine the Excel version and file extension/format
        FileExtStr = ".xlsx": FileFormatNum = 51
        
        ' Change all cells in the worksheet to values if you want
        With Destwb.Sheets(1).UsedRange
            .Value = .Value
        End With
        
        Application.CutCopyMode = False
        
        ' Save the new workbook/Mail it/Delete it
        TempFilePath = Environ$("temp") & "\"
        TempFileName = ws.Name & " Intercompany Variance " & Format(Now, "dd-mmm-yy h-mm-ss")
        
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        With Destwb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ws.Range("L1:L1")
                .CC = ""
                .BCC = ""
                .Subject = "Variance Report: " & ws.Name
                Stringbody = "Hi " & ws.Range("K1").Value & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Attached, please find the Intercompany Variance report for " & ws.Name & "." & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Please advise once corrected." & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Regards" & vbNewLine & vbNewLine
                Stringbody = Stringbody & "Howard" ' Replace with your name or signature
                
                .Body = Stringbody
                .Attachments.Add Destwb.FullName
                .Display ' Use .Send to send automatically or .Display to check email before sending
            End With
            On Error GoTo 0
            .Close SaveChanges:=False
        End With
        
        ' Delete the file you have sent (optional)
        ' Kill TempFilePath & TempFileName & FileExtStr
        
        Set OutMail = Nothing
        Set OutApp = Nothing
        
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        
    End Sub

+ 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] date format attached d/mm/yyyy attached with hh:mm:ss
    By farrukh in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-14-2017, 09:51 AM
  2. Please see attached file
    By XOR LX in forum The Water Cooler
    Replies: 126
    Last Post: 10-05-2016, 02:27 PM
  3. Do I need to use IF, AND, OR Functions together? Example attached
    By Wilgoss in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 03-30-2016, 06:46 AM
  4. Sum If Question - Example Attached
    By leviathan86 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-31-2013, 02:19 PM
  5. Example Attached (Vlookup?)
    By cjwescott in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-11-2012, 06:23 PM
  6. Replies: 0
    Last Post: 05-16-2012, 04: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