Hey all.
I've been running this code I found online to have a button for composing an email using selected data from one of my spreadsheets. Everything runs fine; the only tiny problem I'm having is that after creating the email message, it jumps back to the worksheet instead of staying on the open email. I tried looking over the code to figure out why, but I can't seem to isolate what's doing it. Could someone take a look and help me figure out how to stay on the new email instead of coming back to Excel? Thanks.
I've been running this code I found online to have a button for composing an email using selected data from one of my spreadsheets. Everything runs fine; the only tiny problem I'm having is that after creating the email message, it jumps back to the worksheet instead of staying on the open email. I tried looking over the code to figure out why, but I can't seem to isolate what's doing it. Could someone take a look and help me figure out how to stay on the new email instead of coming back to Excel? Thanks.
VBA Code:
Sub Send_Email()
ThisWorkbook.Sheets("Summary").Activate
Application.ScreenUpdating = False
Dim outapp As Object
Dim outmail As Object
Dim bdy As Range
Set outapp = CreateObject("outlook.application")
Set outmail = outapp.createitem(0)
Set bdy = Sheets("Summary").Range("B1" & lastCol & lastRow).SpecialCells(xlCellTypeVisible)
On Error Resume Next
With outmail
.To = Sheets("Summary").Range("F1").Value
.Subject = "TFM " & Format(Date, "yyyy.mm.dd") & " " & "Fresh Receiving"
.display
.htmlbody = RangetoHTML(bdy) & .htmlbody
End With
Application.ScreenUpdating = True
End Sub
Function RangetoHTML(Rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
Rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
Sheets(Sheets.Count).Select
End Function