Dim rst
Dim XL As Excel.Application
Set XL = CreateObject("excel.application")
Dim vFile
vFile = "J:\Test Template.xlsx"
Set rst = CurrentDb.OpenRecordset("Test")
If rst.RecordCount = 0 Then
Dialog.Box "No Requests Today!", vbInformation, "Database Message"
Call fncLogOTHER
Me.LastRefreshDateTime.Form.Recalc
Else
rst.MoveLast
Dialog.Box "A Total Of: " & rst.RecordCount & " Requests Found And Will Be Emailed!", vbInformation, "Database Message"
rst.MoveFirst
With XL
.Visible = False
.Workbooks.Open vFile
.Sheets("Other").Select
.Range("A4").Select
.ActiveCell.CopyFromRecordset rst
.ActiveWorkbook.SaveAs filename:=("C:\Test Notification " & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx"), password:="Test"
.ActiveWorkbook.Close
.Application.Quit
Dim signature As String
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail ' This creates a blank email and captures the users default signature.
.BodyFormat = olFormatHTML
.Display
End With
signature = OutMail.HTMLBody
strBodyText = "Hi,<br>" & _
"Please find attached picking requests.<br>" & _
"Let me know if you have problems.<br>" & _
"<br><br>Best wishes,<br>"
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Request Notification"
.HTMLBody = strFntNormal & strBodyText & strTableBody & "<br><br>" & signature
.Attachments.Add "C:\Test Notification " & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx"
If Dir("C:\Test Notification " & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx") <> "" Then
'Send Email
.Send
'outlook tidy up
Set OutMail = Nothing
Set OutApp = Nothing
Kill " C:\Test Notification " & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx"
Dialog.Box "All Data has been exported and has been sent", vbInformation, "Task Complete"
Call fncLogOTHER
Me.LastRefreshDateTime.Form.Recalc
Else
'Don't sent email
Dialog.Box "email was not sent as attachment was missing - please try again", vbInformation, "Something went wrong!"
End If
End With
End With
End If
End Sub