Compose Email VBA Help

Ryusui

New Member
Joined
Apr 15, 2006
Messages
33
Office Version
  1. 365
Platform
  1. Windows
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.

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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Comment out or remove the last line of code in the FUNCTION and see if that makes a difference.

VBA Code:
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
'Sheets(Sheets.Count).Select
End Function
 
Upvote 0

Forum statistics

Threads
1,224,735
Messages
6,180,635
Members
452,992
Latest member
TokugawaIesuma

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