Reputation: 143072
I have about 30 .accdb
files, all contain a report with the same name. I'd like to automate saving the reports as PDFs for subsequent printing. That's all, no queries, no modification, just print the saved report to a PDF.
I can open the db successfully, but I'm not sure how to do the rest.
import win32com.client
a = win32com.client.Dispatch("Access.Application")
a.visible = 1
filename = r'C:\bla\Exam_1\PrintTest\db.accdb'
db = a.OpenCurrentDatabase(filename)
report_name = 'My_Report'
a.Quit()
FWIW, I'm a teacher, and this will help me with grading submissions and take the burden off stressed students printing at the end of an exam. Using Windows 10.
I'm open to using any other scripting language (powershell?) that will help me easily automate this task.
Upvotes: 3
Views: 1539
Reputation: 32672
You can use DoCmd.OpenReport
to automatically open and print reports.
import win32com.client
a = win32com.client.Dispatch("Access.Application")
a.visible = 1
filename = r'C:\bla\Exam_1\PrintTest\db.accdb'
db = a.OpenCurrentDatabase(filename)
report_name = 'My_Report'
a.DoCmd.OpenReport('My_Report')
a.Quit()
By default, DoCmd.OpenReport
will print the report to the printer specified in the report settings. I have code lying around to open it in print preview and specify a printer as well.
If you want to export to a PDF, you can automate that as well:
import win32com.client
a = win32com.client.Dispatch("Access.Application")
a.visible = 1
filename = r'C:\bla\Exam_1\PrintTest\db.accdb'
db = a.OpenCurrentDatabase(filename)
report_name = 'My_Report'
a.DoCmd.OutputTo(3, report_name, r'PDF Format (*.pdf)', r'C:\Path\To\file.pdf')
a.Quit()
Upvotes: 5