Do yourself a big favour.Give your controls meaningful names. Command nnn is not going to mean anything to anyone, including you 6 months down the road.Please also post code within code tags to retain indentation which I hope you are using.
From chatgpt.com
Code:
Sub ExportReportToPDFAndEmail()
Dim objOutlook As Object
Dim objEmail As Object
Dim strReportName As String
Dim strPDFPath As String
Dim strFileName As String
Dim strEmailTo As String
Dim strCriteria As String
Dim InvoiceID As Long
' Set your report name
strReportName = "rptInvoice" ' Change to your report name
' Get the Invoice ID (You can change how you pass the record)
InvoiceID = Forms!frmInvoices!InvoiceID ' Adjust form & field names
' Define the PDF file name and path
strFileName = "Invoice_" & InvoiceID & ".pdf"
strPDFPath = CurrentProject.Path & "\" & strFileName
' Export the report to PDF for the specific record
strCriteria = "[InvoiceID] = " & InvoiceID ' Change field name if needed
DoCmd.OpenReport strReportName, acViewPreview, , strCriteria
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strPDFPath
DoCmd.Close acReport, strReportName
' Create Outlook email
Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(0)
' Define recipient email (Adjust as needed)
strEmailTo = Forms!frmInvoices!CustomerEmail ' Change field name
' Configure email details
With objEmail
.To = strEmailTo
.Subject = "Invoice #" & InvoiceID
.Body = "Dear Customer," & vbCrLf & vbCrLf & _
"Please find attached your invoice." & vbCrLf & vbCrLf & _
"Best Regards," & vbCrLf & "Your Company Name"
.Attachments.Add strPDFPath ' Attach the PDF
.Display ' Change to .Send to send automatically