A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Thought that you might be interested in a modification to the code.
The following code now adds the borders to the temporary file within the UDF code instead of altering the original source data worksheet. (It is not good programming to change the source data.)
Private Sub Workbook_Open()
'send automated email when workbook is open
Dim olkObj As Object
Dim olkEm As Object
Dim strbody As String
Dim sh As Worksheet
Dim Lr As Integer
Dim rngToSend As Range
Set sh = ThisWorkbook.Sheets("Payments Due")
With sh
Lr = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Set rngToSend = sh.Range("A1:C" & Lr)
Set olkObj = CreateObject("Outlook.Application")
Set olkEm = olkObj.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
ThisWorkbook.Name & "" & vbNewLine & _
"was opened by" & vbNewLine & _
Environ("username") & " "
'On Error Resume Next 'Should not be required
With olkEm
.To = ""
.CC = ""
.BCC = ""
.Subject = "Payments Due"
.HTMLBody = strbody & vbNewLine _
& RangetoHTML(rngToSend)
.Display 'Suggest use .Display for testing purposes and can close email without saving
'.Send 'For production code delete .Display and uncomment .Send
End With
Set olkEm = Nothing
Set olkObj = Nothing
End Sub
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
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
Call InsertBorders(.Cells(1).CurrentRegion) 'Optional to insert borders in email
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=")
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
Sub InsertBorders(rngForBorders As Range)
'Inserts medium outline border and Thin internal borders
rngForBorders.Borders(xlDiagonalDown).LineStyle = xlNone
rngForBorders.Borders(xlDiagonalUp).LineStyle = xlNone
With rngForBorders.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
With rngForBorders.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
With rngForBorders.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
With rngForBorders.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
With rngForBorders.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With rngForBorders.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
End Sub