Rich (BB code):
VBA Code:
Sub createandmailDailySummaryExcel()
Dim OutApp As Object
Dim OutMail As Object
Dim WB As Workbook
Dim ws As Worksheet
Dim strbody As String
Dim sEmailAddress As String
'Makes a copy of the active sheet and saves it to a temporary file
ActiveSheet.Copy
Set WB = ActiveWorkbook
FileName = "Daily Summary.xlsx"
On Error Resume Next
The following code is failing at the WBSaveAs line.
Kill "C:\" & FileName
On Error GoTo 0
WB.SaveAs FileName:="C:\" & DailySummary.xlsx
'Create a temporary workbook and copy the active worksheet to it
Set WB = Workbooks.Add
ws.Copy Before:=WB.Sheets(1)
'Save the temporary workbook
WB.SaveAs "C:\Temp\DailySummary.xlsx"
'Create the email object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Compose the email
With OutMail
.To = sEmailAddress 'Specify the email address of the recipient
.Subject = "Daily Summary Excel Sheet"
.Body = "Attached is an Excel copy of the Daily Summary"
.Attachments.Add WB.FullName
.Display
End With
'Clean up
Set OutMail = Nothing
Set OutApp = Nothing
WB.Close SaveChanges:=False
Kill "C:\Temp\TempWorkbook.xlsx"
Set WB = Nothing
Set ws = Nothing
End Sub