Hi Guys, I have found 2 functions to try and output a specific Excel Sheet to Email Body, watched the video of it and followed apart from renaming my Dims, I am coming up with Sub Or Function not defined ? (RangeToHTML) not defined
I try and use the run option in Function in EmailRange
One final question, when you kind people out there can confirm where i am wrong, I am guessing if I want to call this from a command button, i would need to open the Excel File not visible then run the EmailRange function ?
Code:
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
Code:
Sub EmailRange()
Dim OutApp As Object, OutMail As Object
Dim iRowCount As Integer, iColCount As Integer
Dim rngSheet As Range
Dim strBody As String, strGreet As String, strExitLine As String, strUser As String, strMsg As String
strGreet = "Hello <br><br>"
strMsg = "DMT Holidays " & Format(Now(), "yyyy") & " has been updated, here is an updated version <br><br>"
strExitLine = "Kind Regards <br><br>"
strUser = Forms!frmMainMenu!txtLogin
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
iRowCount = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown)))
iColCount = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
Set rngSheet = Sheets("PRINT UPDATE").Range(Cells(1, 1), Cells(iRowCount, iColCount))
strBody = "<BODY Style = font-Size:11pt;font-family:Times New Roman>" & _
strGreet & strMsg & "<br>" & strExitLine & "<br>" & strUser
On Error Resume Next
With OutMail
.To = "myemailaddress"
.subject = "Holiday Calendar Update"
.Display
.HTMLBody = strBody & RangetoHTML(rngSheet) & strGreet & .HTMLBody
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub