Can Someone please help I am trying to close the report whether or not the email is sent. This is the code I currently have. The reason I need to close it is because the next one that I do for somereason only changes the report name but keeps the existing data causing the pdfs saved to be the same with a different day. Even when attached in email they are the same and I must close the report and then have the button reopen in order for the data to change.
Code:
Private Sub cmdEmailInvoice_Click()
On Error GoTo cmdEmail_Click_Err
DoCmd.OpenReport "Invoice", acViewPreview, , "[ProjectID]=" & [ID]
'Save report to local drive
Dim strFile As String
strFile = "C:\Invoices\Invoice_" & "#" & [ID] & "_" & Format(Now, "MM-DD-YYYY") & ".pdf"
DoCmd.OutputTo acOutputReport, "Invoice", acFormatPDF, strFile
Dim stReport As String
Dim stWhere As String
Dim stEmail As String
Dim stSubject As String
Dim stEmailMessage As String
Dim ReportCap As String
stEmailMessage = "" & vbCrLf & vbCrLf & "" & vbCrLf & "" 'Email Body
stSubject = "Invoice" 'Email Subject
stReport = "Invoice" 'Original name of the Report
ReportCap = "Invoice" 'Current Report Caption
stEmail = "[EMAIL="e-mail@e-mail.xx"]e-mail@e-mail.xx[/EMAIL]" 'E-mail adresses or (Me.E-mail fieldname)
'DoCmd.OpenReport stReport, acViewPreview, "", stWhere, acWindowNormal, "" 'Opens the report
Reports!Invoice.Caption = "Invoice_#" & [ID] 'Renames the Report
DoCmd.SendObject acSendReport, stReport, "PDFFormat(*.pdf)", stEmail, , , stSubject, stEmailMessage, True, "" 'Send out the E-mail
DoCmd.Close acReport, stReport, acSaveNo
cmdEmail_Click_Exit:
Exit Sub
cmdEmail_Click_Err:
Select Case Err.Number
Case 2501
MsgBox "Email Canceled by user.", vbInformation
Case Else
MsgBox "Error " & Err.Number & " " & Err.Description
End Select
Resume cmdEmail_Click_Exit
End Sub
Last edited: