How do you change the name of email attachment?

Minddumps

Registered User.
Local time
Today, 03:35
Joined
Jul 5, 2011
Messages
73
SOLVED--SEE BELOW TRAFFIC

I was given the suggestion to save the report to desktop, then email that saved doc (pdf), then delete the saved doc.

My reports open up using the same query that my form was run on. I'm assuming I need to somehow combine the two codes that I have for save and then email but am stuck on how to email the saved doc, and also how to delete the file that was saved. Is anyone willing to advise?

This is the code I have so far:

Code:
Private Sub EmailAll_Click()
On Error GoTo Err_Email_Click
 
    Dim strReport As String
    Dim stEmail As String
    Dim stSubject As String
    Dim MyFilename As String
    Dim MyPath As String
    Dim MyFilename As String
    Dim ShowPdf As Boolean
 
    strReport = "rpt-INSPECTION-WORKSHEETS"
    Me.Refresh
 
'I place the file on the user's desktop
MyPath = "C:\Documents and Settings\" & Environ("USERNAME") & "\Desktop\"
 
'State the filename. Here I use YYYY-DD-MM-RecordID Group--Inspection Checklist.pdf
MyFilename = Format(Date, "yyyy") & _
                 "-" & Format(Date, "mm") & "-" & Format(Date, "dd") & _
                 "-" & [RecordID] & " Group--Inspection Checklist" & ".pdf"
 
'Open the report using the form's query and save.
    If Me.Filter = "" Then
        MsgBox "Apply a filter to the form first."
    Else
        DoCmd.OpenReport "rpt-INSPECTION-WORKSHEETS", acViewReport, , Me.Filter
    Reports(strReport).Visible = False
    End If
 
ShowPdf = False
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, MyPath & "\" & MyFilename, True
 
 
'Starting Email code
    stSubject = "Inspection Sheet " & Me!RecordID & "group"
 
'Here is where I need the code to attach the previously created form on the desktop to an email
 
Exit_Email_Click:
    Exit Sub
 
Err_Email_Click:
    MsgBox Err.Description
    Resume Exit_Email_Click
 
'Then here is where I need the code to delete that file on the desktop
 
'Finally, close our previewed report
DoCmd.Close acReport, strReport, acSaveYes
End Sub
 
Last edited:
Take a look at the solution i posted here.

hth
Chris


Ahhh! You are my new HERO!! I was going crazy trying to figure this out! lol Thank you so much! :)

For others looking for my full code used here it is:

Code:
Private Sub MailReports_Click()
    On Error GoTo Err_Email_Click
 
    Dim strReport As String
    Dim MyFilename As String
    Dim MyPath As String
    Dim ShowPdf As Boolean
 
    strReport = "rpt-INSPECTION-WORKSHEETS"
    Me.Refresh
 
'I place the file on the user's desktop
MyPath = "C:\Documents and Settings\" & Environ("USERNAME") & "\Desktop\"
 
'State the filename. Here I use YYYY-DD-MM-RecordID Group--Inspection Checklist.pdf
MyFilename = Format(Date, "yyyy") & _
                 "-" & Format(Date, "mm") & "-" & Format(Date, "dd") & _
                 "-" & [RecordID] & " Group--Inspection Checklist" & ".pdf"
 
'Open the report using the form's query and save.
    If Me.Filter = "" Then
        MsgBox "Apply a filter to the form first."
    Else
        DoCmd.OpenReport "rpt-INSPECTION-WORKSHEETS", acViewReport, , Me.Filter
    Reports(strReport).Visible = False
    End If
 
ShowPdf = False
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, MyPath & "\" & MyFilename, False
 
 
'Starting Email code
'Here is where the code will attach the previously created form on the desktop to an email
 
Dim appOutLook As Object
    Dim MailOutLook As Object
 
    'create the report temporarily
    DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, MyPath & "\" & MyFilename, False
 
    'assign our object references
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
 
    With MailOutLook
        'set the recipient list
        .To = ""
        .CC = "email"
        'set the subject
        .Subject = "Inspection Sheet for " & Me!RecordID & " group"
 
        'set the body text
        .body = "Please view the attached Inspection Sheet Group for " & Me!RecordID & "."
 
        'add the reports we created
        .attachments.Add MyPath & "\" & MyFilename
 
        'Display the email before sending
        .Display
    End With
 
    'tidy up..
 
    'get rid of our object references
    Set appOutLook = Nothing
    Set MailOutLook = Nothing
    'delete our temporary files
    Kill MyPath & "\" & MyFilename
 
 
 
Exit_Email_Click:
    Exit Sub
 
Err_Email_Click:
    MsgBox Err.Description
    Resume Exit_Email_Click
 
'Finally, close our previewed report
DoCmd.Close acReport, strReport, acSaveYes
End Sub
 

Users who are viewing this thread

Back
Top Bottom