Saving report to pdf fails with error 2501 "The OutputTo action was canceled" (1 Viewer)

AndrewS

Registered User.
Local time
Today, 11:54
Joined
Feb 21, 2017
Messages
30
Saving report to pdf fails with error 2501 "The OutputTo action was canceled"

Hello,

I am trying to put a "Open as PDF" button in a report's header, so that users can share the report as a pdf.

I hide the ribbon from users, so it needs to be done as a button with vba.

Every time I try to run it , I get the Error 2501 The OutputTo action was canceled message.

A quick google reveals that a fair number of people have had the same issue, but I haven't yet seen a solution that works.

I am using Access 2016 on Windows 10.

The code on my button is as follows:
Code:
Private Sub btnPDF_Click()
  
  Dim exportpath As String
  'exportpath = "%APPDATA%\EDB\ToDoList.pdf"
  exportpath = "C:\Users\" & TheCurrentUserName() & "\AppData\Roaming\EDB"
  DoCmd.OutputTo acOutputReport, , acFormatPDF, exportpath
End Sub

As you can see I wondered if using an environment variable was the problem. It wasn't, even using the full path gives the same error message.
(TheCurrentUserName() is a function that returns the current windows logon user name.)

I can save the report as a pdf using the export to pdf option the export section of the External Data menu on the ribbon, and I can save it to the specified directory.

It also exports to Excel fine using vba. I might give in and tell them it's either print or Excel.

Any solutions?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:54
Joined
Aug 30, 2003
Messages
36,123
Re: Saving report to pdf fails with error 2501 "The OutputTo action was canceled"

You didn't specify the file name.
 

AndrewS

Registered User.
Local time
Today, 11:54
Joined
Feb 21, 2017
Messages
30
Re: Saving report to pdf fails with error 2501 "The OutputTo action was canceled"

You didn't specify the file name.
Oh for heaven's sake. I am an idiot. <slaps self around head>

Though I did have it in originally when I was trying to use the %appdata% evironment variable, so I guess I was part right, and access doesn't understand ye olde environment variables.

For sake of completeness in case anyone makes the same schoolboy error as me, here is the working code.

Code:
Private Sub btnPDF_Click()
  
  Dim exportpath As String
  exportpath = "C:\Users\" & TheCurrentUserName() & "\AppData\Roaming\EDB\ToDoList.pdf"
  DoCmd.OutputTo acOutputReport, , acFormatPDF, exportpath, True
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:54
Joined
Aug 30, 2003
Messages
36,123
Re: Saving report to pdf fails with error 2501 "The OutputTo action was canceled"

We've all done it. ;)
 

bsm2th

New member
Local time
Today, 06:54
Joined
Apr 19, 2020
Messages
4
Does your report do anything dynamic while printing the data? If there is anything that looks at the data and changes the report because of it, this won't work. OutputTo just sprays data that is already there. OpenReport will allow where conditions to pick what data you want and it will allow you to look at the data and change the report based on what it sees. Here is some code

FileName = "c:\Reports\" & strReport & " for Week " & Me.Thisweekbox & " of " & MonthName(Me.datemonth) & " " & Me.dateyear & ".pdf"
Call DoCmd.OpenReport(reportName:=strReport _
, view:=acViewPreview _
, FilterName:=strQuery _
, WhereCondition:=strWhere _
, Windowmode:=acHidden) 'Use acViewNormal to print instead of preview.
If Err = 2501 Then Exit Sub
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, FileName
DoCmd.Close acReport, strReport, acSaveNo

OpenReport here uses a query to filter only the data wanted, then the WhereCondition can change to anything the user picks on the main form of
 

bsm2th

New member
Local time
Today, 06:54
Joined
Apr 19, 2020
Messages
4
the application. After OpenReport makes a pretty report, OutputTo will turn that into a .pdf. The 2501 line will stop things if the report is empty.

Bob <bsm2th@gmail.com>
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:54
Joined
Aug 30, 2003
Messages
36,123
We normally wouldn't wake up a 2 year old thread, particularly when the original poster said it was resolved. ;)
 

Users who are viewing this thread

Top Bottom