I have a VBA code that is copying a range in Excel and pasting into the body of an Outlook email. The code works on several of my colleagues computers but not mine. The code gets as far as creating the .temp file, populating the To:, CC: and Subject but nothing appears in the body. I am trying to paste HTML. I am thinking it’s a setting or something like that, but I’m not sure where to start. Any help on this would be greatly appreciated.
Sub CreateEmail()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
datestr = Date
sbj = "***"
toStr = "****"
Ccstr = "*****"
Dim rng As Range
Set rng = Nothing
Set rng = Range("Flash")
On Error Resume Next
With OutMail
.To = toStr
.Display
.CC = Ccstr
.BCC = ""
.Subject = sbj & datestr
.HTMLBody = RangetoHTML(rng)
End With
On Error GoTo 0
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
Application.ScreenUpdating = False
TempFile = Environ$("temp") & "/" & "temp" & ".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
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
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
Application.ScreenUpdating = True
End Function
I opted to used this method instead. This pastes a range of excel, but not the HTML version. I did not need to use HTML. This works just as good, but without colors and such.