Change name of exported report via VBA (1 Viewer)

gojets1721

Registered User.
Local time
Today, 01:34
Joined
Jun 11, 2019
Messages
429
I'm using some simple code below to export an invoice and attach it to an email in outlook. The code also adds a default email subject and message.

When using this code, the attached invoice retains the same file name as the report name in Access. I was wondering if there's a way in VBA to rename the attached report to something different. I don't want to change the default naming format in Access (i.e. rptInvoice) but it would look a little bit better in Outlook if it was named something cleaner (i.e. Invoice, New Invoice, etc.)

Any suggestions?

Code:
Private Sub btnEmailInvoice_Click()
On Error GoTo btnEmailInvoice_Click_Err

    DoCmd.RunCommand acCmdRefresh
    DoCmd.SendObject acReport, "rptInvoice", "PDFFormat(*.pdf)", "", "", "", "Invoice", "Hello, your invoice is attached.", True, ""
    
btnEmailInvoice_Click_Exit:
    Exit Sub

btnEmailInvoice_Click_Err:
    If Err = 2501 Then
      
    Else
        ' Report the error
        MsgBox Err.Description, vbExclamation
    End If
    
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:34
Joined
Oct 29, 2018
Messages
21,454
Hi. Before you use SendObject, you can open the report in hidden view and then adjust its Caption property. Do the SendObject and then close the hidden report.
 

KitaYama

Well-known member
Local time
Today, 17:34
Joined
Jan 6, 2022
Messages
1,540
In cases like this, I save the report as pdf in a folder, then attach it to a mail and then delete the pdf.
Rich (BB code):
        FileName = GetEnviron("Desktop") & "myPDFName.pdf"
        DoCmd.OpenReport "MyReport", AcView, , , acHidden, Args
        DoCmd.OutputTo acOutputReport, "MyReport", "PDF", FileName
        DoCmd.Close acReport, "MyReport"
 

Users who are viewing this thread

Top Bottom