Option Compare Database
Option Explicit
Private strStartHTML As String
Public strRptName As String
Public Function RptEmailHtml(varPath As Variant, strRptName As String)
On Error GoTo Err_RptEmailHtml
Dim HTMLFile As String
Dim i As Integer
Dim strBody As String
Dim strTemp As String
Dim strflLeft As String
Dim strFirstFile As String
Dim arrFiles
If Len(strRptName & vbNullString) < 7 Then
Call MsgBox("The Report Name needs to be at least 7 characters long " _
& vbCrLf & " so the files being used can be verified." _
, vbExclamation, " Please Make Your Report Name Longer")
GoTo Exit_RptEmailHtml
End If
HTMLFile = varPath & strRptName & ".HTML"
strFirstFile = HTMLFile
DoCmd.OutputTo acOutputReport, strRptName, acFormatHTML, _
HTMLFile, False
strflLeft = Left(strRptName, 7)
arrFiles = Array(Dir(varPath, vbNormal))
Do While arrFiles(i) <> 0
If strflLeft = Left(arrFiles(i), 7) Then
strTemp = ReadHTMLFile(varPath & arrFiles(i), strFirstFile)
strBody = strBody & "<P></P><P></P>" & strTemp
Debug.Print varPath & arrFiles(i)
End If
On Error GoTo StepOut
arrFiles(i) = Dir
Loop
StepOut:
Err.Number = 0
strBody = strStartHTML & strBody
SendHTMLMail (strBody)
Exit_RptEmailHtml:
Exit Function
Err_RptEmailHtml:
Call MsgBox(Err.Description & vbCrLf & "Error Number: " & Err.Number & vbCrLf & _
" In procedure RptEmailHtml of Module basEmail")
Resume Exit_RptEmailHtml
End Function
Sub SendHTMLMail(strBodyText)
Dim strTo As String
Dim olApp As Object
Dim olMail As Object
Dim strSubject As String
Dim strMsg As String
If isAppThere("Outlook.Application") = False Then
Set olApp = CreateObject("Outlook.Application")
Else
Set olApp = GetObject(, "Outlook.Application")
End If
Set olMail = olApp.CreateItem(0)
strMsg = "Here is the Report I Promised."
strSubject = "My Report HTML in Email Body Test"
With olMail
.To = "joe@irjoe.com"
.Subject = "My Report HTML in Email Body Test"
.HTMLBody = strMsg & strBodyText & "</BODY>" & vbNewLine & "</HTML>"
End With
olMail.Display
End Sub
Function ReadHTMLFile(strFileName, strFirstFile As String)
Const ForReading = 1, ForWriting = 2
Dim fso, f, r, i
Dim intNoLines, intStartLine
Dim strTemp
Dim intBody As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(strFileName, ForReading)
Do While f.AtEndOfStream <> True
r = f.ReadLine
If strFileName = strFirstFile Then
If InStr(1, strStartHTML, "<BODY>") = 0 Then
strStartHTML = strStartHTML & r
End If
End If
intBody = InStr(1, r, "<BODY>")
If intBody > 0 Then intStartLine = f.Line + 1
Loop
intNoLines = f.Line
Set f = fso.OpenTextFile(strFileName, ForReading)
i = 1
Do While i <= intNoLines - 2
If i >= intStartLine - 1 Then
strTemp = strTemp & f.ReadLine
Else
f.ReadLine
End If
i = i + 1
Loop
f.Close
ReadHTMLFile = strTemp
End Function
Public Function HtmlNoReportEmail(strTblQryName As String)
Dim olApp As Object
Dim olMail As Object
Dim strMsg As String
Dim sqlString As String
Dim i As Integer
Dim rowColor As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
sqlString = "SELECT * FROM " & strTblQryName & ""
rs.Open sqlString, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
strMsg = "<table border='1' cellpadding='3' cellspacing='3' style='border-collapse: collapse' bordercolor='#111111' width='800'>" & _
"<tr>" & _
"<td bgcolor='#7EA7CC'> <b>Appointment</b></td>" & _
"<td bgcolor='#7EA7CC'> <b>Start Date</b></td>" & _
"<td bgcolor='#7EA7CC'> <b>Start Time</b></td>" & _
"<td bgcolor='#7EA7CC'> <b>End Date</b></td>" & _
"<td bgcolor='#7EA7CC'> <b>End Time</b></td>" & _
"<td bgcolor='#7EA7CC'> <b>Location</b></td>" & _
"<td bgcolor='#7EA7CC'> <b>Notes</b></td>" & _
"</tr>"
i = 0
Do While Not rs.EOF
If (i Mod 2 = 0) Then
rowColor = "<td bgcolor='#FFFFFF'> "
Else
rowColor = "<td bgcolor='#E1DFDF'> "
End If
strMsg = strMsg & "<tr>" & _
rowColor & rs.Fields("Appt") & "</td>" & _
rowColor & rs.Fields("ApptDate") & "</td>" & _
rowColor & rs.Fields("ApptTime") & "</td>" & _
rowColor & rs.Fields("EndDate") & "</td>" & _
rowColor & rs.Fields("EndTime") & "</td>" & _
rowColor & rs.Fields("Location") & "</td>" & _
rowColor & rs.Fields("ApptNotes") & "</td>" & _
"</tr>"
rs.MoveNext
i = i + 1
Loop
strMsg = strMsg & "</table>"
If isAppThere("Outlook.Application") = False Then
Set olApp = CreateObject("Outlook.Application")
Else
Set olApp = GetObject(, "Outlook.Application")
End If
Set olMail = olApp.CreateItem(0)
With olMail
.BodyFormat = 2
.HTMLBody = strMsg
.Recipients.Add "joe@irjoe.com"
.Subject = "Customer Data TEST"
.Display
End With
Set olApp = Nothing
Set olMail = Nothing
End Function
Function ImageInEmailBody()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim strMsg As String
strMsg = strMsg & "<HTML><Body>" & "<img src='cid:freak01.gif'>" & "<br>" & "<img src='cid:freak02.gif' >" & "</BODY></HTML>"
Set olApp = Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.Attachments.Add ("c:\freak01.gif")
.Attachments.Add ("c:\freak02.gif")
.BodyFormat = olFormatHTML
.HTMLBody = strMsg
.Recipients.Add "user@email.com"
.Subject = "YourSubject"
.Display
End With
Set olApp = Nothing
Set olMail = Nothing
End Function
Function DoesFileExist(strFileNamePath As String) As Boolean
Dim strMsg As String
If Len(Dir(strFileNamePath)) > 0 Then
strMsg = strFileNamePath & " file exists."
DoesFileExist = True
Else
strMsg = strFileNamePath & " file does not exist."
DoesFileExist = False
End If
End Function
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
Dim lngAttributes As Long
lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)
If bFindFolders Then
lngAttributes = (lngAttributes Or vbDirectory)
Else
Do While Right$(strFile, 1) = "\"
strFile = Left$(strFile, Len(strFile) - 1)
Loop
End If
On Error Resume Next
FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function
Function FolderExists(strPath As String) As Boolean
On Error Resume Next
FolderExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)
End Function
Function TrailingSlash(varIn As Variant) As String
If Len(varIn) > 0 Then
If Right(varIn, 1) = "\" Then
TrailingSlash = varIn
Else
TrailingSlash = varIn & "\"
End If
End If
End Function
Function isAppThere(appName) As Boolean
On Error Resume Next
Dim objApp As Object
isAppThere = True
Set objApp = GetObject(, appName)
If Err.Number <> 0 Then isAppThere = False
End Function