Strange problem with exporting access report in pdf format

Access 2013 (accdb format) database.

I have to following code to open an access report in preview, export to pdf and then close the report (the szWhere is a filter string):
    'Preview the report
    DoCmd.OpenReport szDocName, acViewPreview, , szWhere
    
    '*****************************************************************************************
    DoCmd.OutputTo acOutputReport, , acFormatPDF, szFileName, False
    '*****************************************************************************************
    
    DoCmd.Close acReport, szDocName

Open in new window


In development it works perfectly.  In the production environment this code has stopped working.  It has been working for ca 18 months without problem.  It fails on the OutputTo line.
Interestingly on the production PC if a report is opened in preview mode manually, then the export to pdf/xps option chosen the report is exported without problem.

I'm stuck - the functionality works when run manually but not automatically.  Any ideas as to what the problem could be and how to get it working again.
LVL 45
AndyAinscowFreelance programmer / ConsultantAsked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
I have tried that but it didn't make any difference.  :-(

Found it.  For some reason the destination folder had been deleted !!  Creating it cures the problem.
0
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
If I had to guess it could be a timing thing. I think the docmd works on the active object, if you haven't specified an object by name. Try being explicit about which opject to export:
 DoCmd.OpenReport szDocName, acViewPreview, , szWhere
    

    DoCmd.OutputTo acOutputReport,szDocName , acFormatPDF, szFileName, False

Open in new window

0
 
Rey Obrero (Capricorn1)Commented:
I had this recurring problem before and prevented it from happening by adding codes to check if the folder exists and if it don't exists, create the folder before running the codes.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
Missing folder.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.