VBA Code Does't Execute When Exporting to PDF

Posted on 2014-02-02
Medium Priority
Last Modified: 2014-02-04
I have a report that contains underlying VBA code. The code hides and shows report objects based on various conditions. When I preview the report, the code executes as planned. When I export the report to PDF, my code doesn't execute. Is there any solution to this problem?
Question by:TechMommy
LVL 25

Expert Comment

ID: 39828811
What we do is instead of exporting to a PDF we print to a PDF printer. We also have a VBA code, and it works
LVL 11

Author Comment

ID: 39828820
Do you print to a PDF printer programmatically, or from the UI? In this application the process of exporting to PDF is executed from a VBA module. It all has to be automated.
LVL 25

Expert Comment

ID: 39828825
We open the report and print it via a VBS script, that runs via the Task Scheduler. Yes, it is automated
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

LVL 28

Expert Comment

ID: 39828858
Put your code in the Detail_Format sub-routine.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    ' Your code
End Sub

Open in new window

LVL 61

Accepted Solution

mbizup earned 1200 total points
ID: 39829171
Hi Alison,

This is similar to printing out or exporting a single record (or otherwise filtered records) in a report.

The catch is to open your report in Preview - so that the VBA code runs/ filtering takes place/ etc, and then use the outputTo command to export the report to PDF:

DoCmd.OpenReport "YourReport", acViewPreview
DoCmd.OutputTo acOutputReport, "YourReport", acFormatPDF, "YourFileName.PDF", True
doCmd.Close acReport, "YourReport"  ' <---  you can do this if the preview is not needed.

Open in new window

LVL 11

Author Closing Comment

ID: 39834278
This solution works great and was simple to implement. I was able to implement and test it in 5 minutes!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.

Join & Write a Comment

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
When we develop an application in Ms Access 2016 we should also try to protect the queries, macros and table links. I know I may not have a permanent solution but for novice users, they will not manage to break your application. Below is the detail …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question