I've been playing with this code that I found which allows me to click a button in an Excel 2010 spreadsheet and generate an email (Outlook 2010) with an attachment. It works fairly well except for one thing: Once the new mail window is closed (either by cancelling or sending the message), I have to select an unprotected cell in the spreadsheet or hit the Escape key before I can interact with the sheet again. I've tried various things to try and programmatically change the focus to one of the cells or ranges, but nothing has worked so far. Anyone have an idea on how to return to the spreadsheet with a cell/range selected and ready to edit?
Code:
Sub Mail_ActiveSheet()
'Working in 2000-2010
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 strCompany As String
Dim strLease As String
Dim strWell As String
Dim strDate As String
Dim strEmailrecipients As String
With Worksheets("Well Optimization Charges")
strCompany = .Range("range_Company").Value
strDate = Format(.Range("range_Date").Value, "mm-dd-yyyy")
strLease = .Range("range_Lease").Value
strWell = .Range("range_Well").Value
End With
With Worksheets("Data")
strEmailrecipients = .Range("table_EmailRecipients")(1)
End With
If strCompany = "" Then
MsgBox "You must enter a company name before sending this file.", vbCritical, "Missing Company Name"
Else
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 2000-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2010, we exit the sub when your answer is
'NO in the security dialog that you only see when you copy
'an sheet from a xlsm file with macro's disabled.
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
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 If
End With
' 'Change all cells in the worksheet to values if you want
' With Destwb.Sheets(1).UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
'TempFileName = "Part of " & Sourcewb.Name & " " _
' & Format(Now, "dd-mmm-yy h-mm-ss")
TempFileName = strCompany & " - " & strLease & " - " & strWell & " - " & strDate
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 = "address@domain.com"
.CC = ""
.BCC = ""
.Subject = TempFileName
.Body = ""
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
'.Send 'or use .Display
.Display
End With
On Error GoTo 0
.Close SaveChanges:=False
End With
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End If
End Sub