Andrew: I figured out something: If i wrire the following code which sends an attachment with email then it works:
Sub Mail_Selection_Range_Outlook_Body(a As String)
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
Dim wbkMyBook As Workbook
Set rng = Workbooks("C:\a.xls").Worksheets("Slide").UsedRange
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.Recipients.Add "ABC"
.Subject = "Reminder"
.Attachments.Add ("C:\a.xls")
.Send
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
But if I want to just display "Slide" sheet of "a.xls" in the body of email as follows then it doesn't do anything but send a blank email:
Sub Mail_Selection_Range_Outlook_Body(a As String)
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
Dim wbkMyBook As Workbook
Set rng = Workbooks("C:\a.xls").Worksheets("Slide").UsedRange
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.Recipients.Add "ABC"
.Subject = "Reminder"
.HTMLBody = RangetoHTML(rng)
.Send
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
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"
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
.DrawingObjects.Visible = True
.DrawingObjects.Delete
End With
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
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=")
TempWB.Close SaveChanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Please help me out to send "Slide" sheet in the body of the email.
Thanks a lot
Bookmarks