Macro to attached sheets without formulas

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have code to attach the folowing sheets in Outlook , but only want to attach these as values and to have the same format as source sheets

Inventory 150 Days +
Dashboard

See my code below
Code:
 Sub Email_Stock_Report()
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .CutCopyMode = False
    End With
   
    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 Strinbody As String
   
    Set Sourcewb = ActiveWorkbook
   
    ' Create a new workbook
    Set Destwb = Workbooks.Add
   
    ' Copy the specified sheets to the new workbook
    Sourcewb.Sheets(Array("Inventory 150 Days +", "Dashboard")).Copy Before:=Destwb.Sheets(1)
   
    ' Loop through each cell in the "Dashboard" sheet to remove formulas and retain formatting
    Dim wsDashboard As Worksheet
    Set wsDashboard = Destwb.Sheets("Dashboard")
   
    ' Remove formulas from the Dashboard sheet
    wsDashboard.UsedRange.Value = wsDashboard.UsedRange.Value
   
    ' Copy and paste values and formats for the "Inventory 150 Days +" sheet
    Dim wsInventory As Worksheet
    Set wsInventory = Destwb.Sheets("Inventory 150 Days +")
   
    ' Paste only values to remove formulas
    wsInventory.UsedRange.Value = wsInventory.UsedRange.Value
   
    ' Copy and paste formats for the "Inventory 150 Days +" sheet
    wsInventory.UsedRange.Copy
    wsInventory.Cells(1).PasteSpecial Paste:=xlPasteFormats
   
    Application.CutCopyMode = False
   
    ' Determine the Excel file extension/format
    FileExtStr = ".xlsm" ' Use .xlsm for macro-enabled workbook
    FileFormatNum = 52     ' Use 52 for .xlsm format
   
    ' Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "" & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
   
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
   
    With Destwb
        ' Delete "Sheet1" in the new workbook
        On Error Resume Next
        .Sheets("Sheet1").Delete
        On Error GoTo 0
       
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = "kevin.james@btr.com;frank.simons@btr.com"
            .CC = ""
            .BCC = ""
            .Subject = "Summary + Group Overaged Stock 150 Days +"
            Strinbody = "Hi Guys" & vbNewLine & vbNewLine
            Strinbody = Strinbody & "Attached please find Dashboard (summary) as well the New & Used 150 days +" & vbNewLine & vbNewLine
            Strinbody = Strinbody & "Regards" & vbNewLine & vbNewLine
            Strinbody = Strinbody & "Howard"
           
            .Body = Strinbody
            .Attachments.Add TempFilePath & TempFileName & FileExtStr
            .Display   ' Use .Send to send automatically or .Display to check email before sending
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With
   
    ' Wait for a few seconds to ensure the email is sent before attempting to delete the file
    Application.Wait Now + TimeValue("00:00:05")
   
    ' Delete the temporary file
    On Error Resume Next
    Kill TempFilePath & TempFileName & FileExtStr
    On Error GoTo 0
   
    Set OutMail = Nothing
    Set OutApp = Nothing
   
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .CutCopyMode = False
    End With
End Sub


Kindly amend my Code
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Create a copy of the worksheet without formulas (i.e. Values only). Then attach the new values-only worksheets to your email?
Is that a workable option for you?
 
Upvote 0
Thanks, but I am looking for VBA to do this for me
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top