I won't need to do a copy, but as the file goes out to a couple of
users, they might just action the macro and hence that is why I'd like
to see the Msb Box, wouldn't look good if they did and then they get
the debug message. As I've said code works fine if I have a sheet to
copy, but if not debugs as above. Full Code is:-
Sub Mail_Database()
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 sh As Worksheet
Dim strbody As String
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheets to a new workbook
First = True
For Each sht In ThisWorkbook.Sheets
Select Case sht.Name
Case "A", "B", "C", "D"
'Do Nothing
Case Else
If First = True Then
'Create New workbook
sht.Copy
Set Destwb = ActiveWorkbook
First = False
Else
With Destwb
sht.Copy after:=.Sheets(.Sheets.Count)
End With
End If
End Select
Next sht
If First = True Then
MsgBox ("There are no Historic Orders to E-Mail")
End If
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
FileExtStr = ".xls": FileFormatNum = 56
End If
End If
End With
TempFilePath = Environ$("temp") & "\"
TempFileName = "Database Extraction from " & Sourcewb.Name & " " &
Format(Now, "dd-mmm-yy h-mm") & "~"
ActiveWindow.TabRatio = 0.908
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr,
FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = "
[email protected]"
.CC = ""
.BCC = ""
.Subject = "Database of Orders"
.Body = ""
.Attachments.Add Destwb.FullName
.ReadReceiptRequested = True
.SendUsingAccount = OutApp.Session.Accounts.Item(1)
.Send
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
End Sub