Hello seaottr,
Copy and paste the Module1 macro code into a new VBA module in your workbook. Change the CommandButton2 in the "Results" worksheet module also.
Module1 Macro Code
'Written: September 22, 2008
'Updated: August 18, 2011
'Author: Leith Ross
'Summary: Send a specfied worksheet range in the body of an Outlook email
' in HTML format.
Sub EmailRangeInHTML(ByVal Recipient As String, ByVal Subject As String, Optional Range_To_Send As Variant)
Dim FSO As Object
Dim HTMLcode As String
Dim HTMLfile As Object
Dim MyApp As Boolean
Dim olApp As Object
Dim Rng As Range
Dim TempFile As String
Dim Wks As Worksheet
Const ForReading As Long = 1
Const olMailItem = 0
Const olFormatHTML = 2
Const UseDefault As Long = -2
On Error GoTo CleanUp
If IsMissing(Range_To_Send) Then
Set Rng = Selection
Else
Select Case TypeName(Range_To_Send)
Case Is = "Range"
Set Rng = Range_To_Send
Case Is = "String"
Set Rng = Evaluate(Range_To_Send)
Case Else
MsgBox "Your Selection is Not a Valid Range."
GoTo CleanUp
End Select
End If
' Copy the worksheet to create a new workbook
Set Wks = Rng.Parent
Wks.Copy
' The new workbook will be saved to the user's Temp directoy
TempFile = Environ("Temp") & "\" & Wks.Name & ".htm"
' If a file by this exists then delete it
If Dir(TempFile) <> "" Then Kill TempFile
' Start Outlook
Set olApp = CreateObject("Outlook.Application")
' Convert the Message worksheet into HTML
With ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
FileName:=TempFile, _
Sheet:=Wks.Name, _
Source:=Rng.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
' Read the HTML file back as a string
Set FSO = CreateObject("Scripting.FileSystemObject")
Set HTMLfile = FSO.OpenTextFile(TempFile, ForReading, True, UseDefault)
' Read in the entire file as a string
HTMLcode = HTMLfile.ReadAll
HTMLfile.Close
' Re-align the HTML code to the left side of the page
HTMLcode = Replace(HTMLcode, "align=center x:publishsource=", _
"align=left x:publishsource=")
' Compose and send the email
Set olEmail = olApp.CreateItem(olMailItem)
With olEmail
.To = Recipient
.Subject = Subject
.BodyFormat = olFormatHTML
.HTMLBody = HTMLcode
.Send
End With
' Exit Outlook
olApp.Quit
CleanUp:
' Did an error occur
If Err <> 0 Then
MsgBox "Run-time error '" & Err.Number & "':" & vbCrLf & vbCrLf & Err.Description
End If
' Close the new workbook and don't save it
ActiveWorkbook.Close SaveChanges:=False
' Delete the Temp File
If Dir(TempFile) <> "" Then Kill TempFile
' Delete the Publish Object
With ActiveWorkbook.PublishObjects
If .Count <> 0 Then .Item(.Count).Delete
End With
' Free memory resources
Set olApp = Nothing
Set olEmail = Nothing
Set FSO = Nothing
End Sub
Sub EmailMyself()
Dim Rng As Range
'Set Rng = ThisWorkbook.Worksheets("Results").Range("A1:J39")
EmailRangeInHTML "LeithRoss@gmail.com", "Sending Range in HTML test"
End Sub
CommandButton2_Click() Event Code
Be sure to change the email address to what you want to use.
Private Sub CommandButton2_Click()
' Working in Office 2000-2007
EmailRangeInHTML "email@email.com", "Team Results - Month-To-Date", Worksheets("Results").Range("A1:J30")
End Sub
Bookmarks