Andy , am using 'ActiveSheet.UsedRange' in my code.Below is my code.I have also attached my worksheet.
Sub Send_Click()
EmailRangeInHTML "abc.xyz@cde.com", "Team Results - Month-To-Date", ActiveSheet.UsedRange
End Sub
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
.Display
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
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
' Free memory resources
Set olApp = Nothing
Set olEmail = Nothing
Set FSO = Nothing
End Sub
Bookmarks