preludeofme
New Member
- Joined
- Dec 4, 2008
- Messages
- 34
Having a problem with a macro that sends out emails formatted as HTML (.HTMLBody). I'm sending from outlook 2010 and it looks fine on my machine and on like a web-based client but when someone tries to reply to my email from outlook 2003 the body of the email is blank.
here's the code i'm using:
The code inserts a selected range into the email in between the intro and the body. i've tried to use the regular .body format instead but the table does not get inserted properly and loses all the formatting.
From what i can tell, the reply in outlook does not reflect HTML tags so it just shows as blank instead of the body of my email. Any ideas?
here's the code i'm using:
Code:
Sub SendEmail()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
intro = "Introduction"
body = "body of email with HTML tags for formatting"
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
countnum = Sheets("Sheet2").Range("A50000").End(xlUp).Row
Set rng = Sheets("Sheet2").Range("A10:E" & countnum)
'Set rng = Sheets("YourSheet").UsedRange
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = Sheets("Sheet2").Range("B2").Value
.CC = ""
.BCC = ""
.Subject = "Subject"
.HTMLBody = intro & RangetoHTML(rng) & body
.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
'------------------------------------------------------------------
'------------------------------------------------------------------
Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
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 workbook to receive the data.
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 an .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 the RangetoHTML subroutine.
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.
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
The code inserts a selected range into the email in between the intro and the body. i've tried to use the regular .body format instead but the table does not get inserted properly and loses all the formatting.
From what i can tell, the reply in outlook does not reflect HTML tags so it just shows as blank instead of the body of my email. Any ideas?