Share via

VBA, saving Report as PDF with custom file name

Anonymous
2017-03-04T01:05:56+00:00

Hello,

I have the below vba and I need help fixing it because not sure what I am doing. 

Below is a written out and a real example of how I need the .pdf file name created. Could you please help me with this. It would be much appreciated.

[Todays Date and Time] EPR Perf **** Completed On Time, Between [BeginDate] and [EndDate]

2017-03-03 17-00 EPR Perf **** Completed On Time, Between 01-01-2017 and 03-03-2017

Option Compare Database

Private Sub ExportAsPDF_EPRBySupervisorReport_Click()

If IsNull(Me.BeginDate) Then

    MsgBox "Please type in a Begin Date"

    Me.BeginDate.SetFocus

    Exit Sub

ElseIf IsNull(Me.EndDate) Then

    MsgBox "Please enter an End Date"

    Me.EndDate.SetFocus

    Exit Sub

End If

Dim strFileName      As String

    Dim strReport        As String

    strReport = "rpt_CSDPerfMeas-EPRsCompletedOnTime"

    strFileName = "EPR Perf **** Completed On Time " & " Between " & _

Format(Me.BeginDate, "YYYY-MM-DD@HH-NN") & " and " & _

Format(Me.EndDate, "YYYY-MM-DD@HH-NN") & " and " & _PdfSaveDate " & _

Format(Now(), "YYYY-MM-DD@HH-NN") & ".pdf"

    strFileName = CurrentProject.Path & "\EXPORTS of Employee Log Database\Excel Export" & strFileName

    DoCmd.OpenReport strReport, acViewPreview

    DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName, False

    ' And now set the filename of the .xls file

    strFileName = Me.LstBox_ChooseDivision & " Agenda " & _

                  Me.AgendaMeetingLocation & ", " & _

                  Me.AgendaAttendees & ", ExcelSaveDate " & _

                  Format(Now(), "YYYY-MM-DD@HH-NN") & ".xls"

    strFileName = CurrentProject.Path & "\EXPORTS of Employee Log Database\Excel Export" & strFileName

    DoCmd.OutputTo acOutputReport, strReport, acFormatXLS, strFileName, False

    DoCmd.Close acReport, strReport

    Call OpenFolderPaTH

End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-03-07T19:31:12+00:00

You have an extra underscore prior to the PdfSaveDate.  In that same spot there appears to be an ampersand and quotation that may be out of order or extra.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2017-03-04T15:40:10+00:00

Hmm, I'm a little confused do you want one report or two?  For the .PDF try *Oh, no asterisks allowed in File Names, so I removed them.)...

    Dim strFileName      As String

    Dim strReport        As String

    'First let's open the report

    strReport = "rpt_CSDPerfMeas-EPRsCompletedOnTime"

    DoCmd.OpenReport strReport, acViewPreview

    'Now let's save the open report to .PDF

    strFileName = "EPR Perf Completed On Time " & " Between " & _

Format(Me.BeginDate, "YYYY-MM-DD@HH-NN") & " and " & _

Format(Me.EndDate, "YYYY-MM-DD@HH-NN") & " and " & _PdfSaveDate " & _

Format(Now(), "YYYY-MM-DD@HH-NN") & ".pdf"

     strFileName = CurrentProject.Path & "\EXPORTS of Employee Log Database\Excel Export" & strFileName

     DoCmd.SelectObject acReport, strReport

     DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName, True

     DoCmd.Close acReport, strReport

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-03-07T17:46:15+00:00

    Ok I copied in your recommendation and now below is the entire VBA screen from top to bottom, but I don't know why the bold section below continues to be red in the VBA screen. When I debug the below I get an error that says "Compile Error: Duplicate declaration in current scope" in the Italic bold below.

    Option Compare Database

    Private Sub ExportAsPDF_EPRBySupervisorReport_Click()

    If IsNull(Me.BeginDate) Then

        MsgBox "Please type in a Begin Date"

        Me.BeginDate.SetFocus

        Exit Sub

    ElseIf IsNull(Me.EndDate) Then

        MsgBox "Please enter an End Date"

        Me.EndDate.SetFocus

        Exit Sub

    End If

    Dim strFileName      As String

        Dim strFileName      As String

        Dim strReport        As String

        'First let's open the report

        strReport = "rpt_CSDPerfMeas-EPRsCompletedOnTime"

        DoCmd.OpenReport strReport, acViewPreview

        'Now let's save the open report to .PDF

      strFileName = "EPR Perf Completed On Time " & " Between " & _

    Format(Me.BeginDate, "YYYY-MM-DD@HH-NN") & " and " & _

    Format(Me.EndDate, "YYYY-MM-DD@HH-NN") & " and " & _PdfSaveDate " & _

    Format(Now(), "YYYY-MM-DD@HH-NN") & ".pdf"

         strFileName = CurrentProject.Path & "\EXPORTS of Employee Log Database\Excel Export" & strFileName

          DoCmd.SelectObject acReport, strReport

          DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName, True

         DoCmd.Close acReport, strReport

         Call OpenFolderPaTH

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-03-04T21:27:58+00:00

    No problem... I'll be around!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-03-04T20:22:30+00:00

    Hello Gina,

    I must have added the asterisks by mistake.

    the VBA has two outputs, both PDF and Excel, but I figure if I can help with the PDF part I can wing the Excel part.

    I'll try your suggestion on Monday and get back to you.

    Iram

    Was this answer helpful?

    0 comments No comments