2
votes

I have a macro that pastes an Excel range into an email. The range has conditional formatting to turn the background color and font color black to hide information.

When pasted into Outlook the font changes from black to white on the areas with black backgrounds.

Sub OpenOutlookEmail()
Dim OutApp As Object
Dim outMail As Object
Dim rng As Range
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set outMail = OutApp.CreateItem(0)
Set rng = ActiveSheet.Range("A1:P35")
On Error Resume Next
With outMail
    .To = "coworkers@me.com"
    .CC = 
    .BCC = ""
    .Subject = "Subject"
    .HTMLBody = "Hello," & vbNewLine _
      vbNewLine & "Body" & _
      RangetoHTML(rng) & "Thanks!" 
    .Display
End With
On Error GoTo 0
Set outMail = Nothing
Set OutApp = Nothing
End Sub

I redacted some information.

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, , True, 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

Is it possible to send as picture or to change a setting in Outlook so it doesn't reformat the text?

1
Black font on black background or white font on white background is cheezy. Change the number format to ;;;.user4039065
@Jeeped Pleased elaborate, where would I make this change? I know it is cheezy but it is effective for my goal in this report.Ziggus
Seems to me it would be in the same place 'which turns the background color and font color black'.user4039065
You should also populate your export table with the cell's .Text property instead of the .Value.user4039065
@Jeeped Wow, that went right over my head... Adding ;;; fixed the issue, thanks!Ziggus

1 Answers

2
votes

Changing to a black font on black background or white font on white background may be the most obvious method to 'hide' cell values but it certainly isn't the best method.

Change the cell number format to a custom number format of ;;; either manually or with condition formatting. This renders the cell display appear blank while retaining the underlying value.