Hi, so i have this code which is run from a button i need to stop the email from sending if no attachment has been made..
Dim rst
Dim XL As Excel.Application
Set XL = CreateObject("excel.application")
Dim vFile
vFile = "Template File Location Here"
Set rst = CurrentDb.OpenRecordset("All")
If rst.RecordCount = 0 Then
Else
rst.MoveLast
Dialog.Box "A Total Of: " & rst.RecordCount & " Records Found And Will Be Emailed!", vbInformation, "Database Message"
rst.MoveFirst
With XL
.Visible = False
.Workbooks.Open vFile
.Sheets("SSS").Select
.Range("A4").Select
.ActiveCell.CopyFromRecordset rst
.ActiveWorkbook.SaveAs filename:=("File Name and Location here & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx"), password:="Test”
.ActiveWorkbook.close
.Application.Quit
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi,<br>" & _
"Please find attached xx Report.<br>" & _
"Let me know if you have problems.<br>" & _
"<br><br>Thanks,<br>"
On Error Resume Next
With OutMail
'.Display
.To = ""
.CC = "
.BCC = ""
.Subject = "Test"
.HTMLBody = strbody & "<br>" & .HTMLBody
.Attachments.Add "File name and locatin here" & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx"
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Also would be good to know how to get it to add the Outlook Signatures
Dim rst
Dim XL As Excel.Application
Set XL = CreateObject("excel.application")
Dim vFile
vFile = "Template File Location Here"
Set rst = CurrentDb.OpenRecordset("All")
If rst.RecordCount = 0 Then
Else
rst.MoveLast
Dialog.Box "A Total Of: " & rst.RecordCount & " Records Found And Will Be Emailed!", vbInformation, "Database Message"
rst.MoveFirst
With XL
.Visible = False
.Workbooks.Open vFile
.Sheets("SSS").Select
.Range("A4").Select
.ActiveCell.CopyFromRecordset rst
.ActiveWorkbook.SaveAs filename:=("File Name and Location here & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx"), password:="Test”
.ActiveWorkbook.close
.Application.Quit
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi,<br>" & _
"Please find attached xx Report.<br>" & _
"Let me know if you have problems.<br>" & _
"<br><br>Thanks,<br>"
On Error Resume Next
With OutMail
'.Display
.To = ""
.CC = "
.BCC = ""
.Subject = "Test"
.HTMLBody = strbody & "<br>" & .HTMLBody
.Attachments.Add "File name and locatin here" & Format(Now(), "DD-MMM-YYYY hhmm ") & ".xlsx"
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Also would be good to know how to get it to add the Outlook Signatures
Last edited: