Rename a pdf attached to en e-mail (1 Viewer)

Holly_Associated

Registered User.
Local time
Today, 02:47
Joined
Mar 21, 2013
Messages
53
Hi everyone,

I have a problem with some code, I am quite new to VBA! I have searched the forums and tried a few things myself and cannot find an answer. I'm using 2013.

The first version of my code works great. It opens the correct report and attaches it to an e-mail as a pdf with all of my text etc.

Code:
Private Sub btnEMail_Click()
On Error GoTo errHandler
    Dim strReport As String
    Dim vMsg As String
    Dim vSubject As String
    Dim strWhere As String
    strReport = "RptJobDSD"
    strWhere = "DSDID = " & Forms!FrmMenu!NavigationSubform!SubFrmJobDSD!DSDID
    vMsg = Trim("Please find attached your daily site diary for ")
    vSubject = Trim("Daily Site Diary for ")
    DoCmd.OpenReport strReport, acViewPreview, , strWhere
    DoCmd.SendObject acSendReport, strReport, acFormatPDF, , , , vSubject, vMsg, True
    
exitOnErr:
    Exit Sub
errHandler:
    If Err.Number <> 2501 Then MsgBox "Error (" & Err.Number & ") - " & Err.Description, vbCritical
    Resume exitOnErr
End Sub

The problem I have is I would like to rename the pdf, so instead of "RptJobDSD" everytime, it can have some fields [DSDDate] and [JobID] etc.
Before trying the fancy field stuff I thought I'd try just "DSD Test" and see if I could rename it in the first place!

I added this and am getting - Error(32004) - The control name 'RptJobDSD' is misspelled or refers to a control that doesn't exist.
Code:
DoCmd.OpenReport strReport, acViewPreview, , strWhere
    [COLOR=blue]DoCmd.SetProperty strReport, acPropertyCaption, "DSD Test"
[/COLOR]    DoCmd.SendObject acSendReport, strReport, acFormatPDF, , , , vSubject, vMsg, True

My confusion comes as the OpenReport and SendObject recognise strReport why doesn't SetProperty?! :banghead:
 

pr2-eugin

Super Moderator
Local time
Today, 02:47
Joined
Nov 30, 2011
Messages
8,494
Because SetProperty method can only change Controls property like Visibility, Enabled etc. Not the name of the Object.. What you need to do is Export the Report to PDF, using DoCmd.OutputTo, then use the generated PDF in a Email code, plenty floating around in the internet..
 

Holly_Associated

Registered User.
Local time
Today, 02:47
Joined
Mar 21, 2013
Messages
53
Hi Paul,
Thank you for your help with this. The report's caption is used as the pdf name so I only wanted to change the caption, not the name of the report. I have used the following code in the report's on open and now it opens with the dynamic name I want :) For some reason it didn't work after the OpenReport in the on click code? I appear to have solved my own problem! There's a first time for everything I suppose :D

Code:
Private Sub Report_Load()
DoCmd.SetProperty RptJobDSD, acPropertyCaption, Trim("DSD - " & JobID & " - " & Format(DSDDAte, "yy-mm-dd"))
End Sub
 

Users who are viewing this thread

Top Bottom