cactus_ritter
New Member
- Joined
- Jul 31, 2023
- Messages
- 2
- Office Version
- 2016
- Platform
- Windows
I am using the Sub Mail_Selection_Range_Outlook_Body() from Ron de Bruin to set up a macro to send an email taking the information from several sheets with reports that I have to send and it works pretty well, but the code doesn't allow conditional formatting to be pasted into outlook.
The behaviour is basically setting up a button to send an email for each of the sheets, so I have the Sub to be called multiple times from another Sub changing the differences from each sheet (like email, department...). This data is sent as arguments to the de Bruin's Sub.
Everything works perfectly as intended, but the conditional formatting is lost. I have tried many things but I can't not make it work for me to keep the conditional formatting.
The behaviour is basically setting up a button to send an email for each of the sheets, so I have the Sub to be called multiple times from another Sub changing the differences from each sheet (like email, department...). This data is sent as arguments to the de Bruin's Sub.
Everything works perfectly as intended, but the conditional formatting is lost. I have tried many things but I can't not make it work for me to keep the conditional formatting.
VBA Code:
Sub Mail_Selection_Range_Outlook_Body(Department As String, DepLeaderName As String, DepLeaderEmail As String)
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim sh1 As Worksheet
Dim MsgText1 As String
Dim MsgText2 As String
Dim SenderName As String
Dim InputDate As Date
Dim CWNumber As Long
Dim CurrentYear As Long
MsgIntro = "Dear " & DepLeaderName & "<br/><br/> "
MsgText1 = "Please find the report below. <br/><br/> "
MsgText2 = "The report uses raw data from today. <br/><br/> Kind regards,<br/> "
SenderName = "Report Team <br/><br/>"
InputDate = Now
CWNumber = Application.WorksheetFunction.WeekNum(InputDate)
CurrentYear = Year(Now)
Set rng = Nothing
On Error Resume Next
Set sh1 = Worksheets(Department)
'You can also use a fixed range if you want
LastRow = sh1.Cells(sh1.Rows.Count, "H").End(xlUp).Row
Set rng = sh1.Range("B7:H" & LastRow).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = DepLeaderEmail
.CC = ""
.BCC = ""
.Subject = "Report for " & Department & " CW" & CWNumber & " " & Year(Now)
.HTMLBody = "<font style=""font-family: Calibri; font-size: 12pt;""/font>" & MsgIntro & MsgText1 & MsgText2 & SenderName & RangetoHTML(rng)
.Send 'or use .Display
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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
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 new workbook to past the data in
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 a 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 RangetoHTML
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 we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function