Hi!
Utilizing Ron de Bruin's code to email Excel range over Outlook in HTML, however could you guys help me understand the conditional formatting part. I want to focus on 3 columns in my range: E; F; G. I have conditional formatting in place for column G based on formula:
e.g. if column F is not 0, format column G (yellow fill). This works just fine in Excel.
Problem occurs when running VBA to email my range over Outlook. VBA hides column F before selecting the range, however I still expect conditional formatting to withstand (which it does - in Excel!). When looking at the email, column F is not showing, as expected, however conditional formatting now works based column E, not column F.
If I avoid hiding column F, conditional formatting works fine, however I prefer to hide column F when selecting the range to be emailed. Could you guys help me in understanding that is causing this?
The code itself is as following:
Utilizing Ron de Bruin's code to email Excel range over Outlook in HTML, however could you guys help me understand the conditional formatting part. I want to focus on 3 columns in my range: E; F; G. I have conditional formatting in place for column G based on formula:
Code:
=F3<>0
Problem occurs when running VBA to email my range over Outlook. VBA hides column F before selecting the range, however I still expect conditional formatting to withstand (which it does - in Excel!). When looking at the email, column F is not showing, as expected, however conditional formatting now works based column E, not column F.
If I avoid hiding column F, conditional formatting works fine, however I prefer to hide column F when selecting the range to be emailed. Could you guys help me in understanding that is causing this?
The code itself is as following:
Code:
Sub Email()
Dim rng As Range
Dim rng2 As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
Set rng2 = Nothing
On Error Resume Next
Set rng = Sheets("Sheet1").Range("Email_range").SpecialCells(xlCellTypeVisible)
Set rng2 = Sheets("Sheet1").Range("Table3").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng2 Is Nothing Then
Call Stop
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 = Range("A2")
.CC = ""
.BCC = ""
.Subject = "Subject line"
.HTMLBody = 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
Application.OnTime Now + TimeValue("00:00:5"), " Close "
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
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
End Function