473,725 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HOWTO: Automate Printing of Access Reports to PDF files

This is the approach I used to automate printing of Microsoft Access
reports to PDF format i.e. unattended and without annoying "Save As..."
dialogs, and - more importantly - without having to use a commercial
program such as Adobe Acrobat and its associated API.

The technique uses Ghostscript and Redirection Port Monitor - two free
programs for creating PDF documents provided free by Russell Lang. The
actual automation requires VBA coding using the FileSystemObjec t,
WScript.Network (the Windows Script Host Network Object) and Access
automation.
INSTRUCTIONS:

1) Install Ghostscript
- An interpreter for the Postscript language and PDF
http://www.cs.wisc.edu/~ghost/

2) Install RedMon - Redirection Port Monitor
- Redirects a special printer port to a program (such as
Ghostscript)
http://www.cs.wisc.edu/~ghost/redmon/

3) Set up virtual PDF printer using Ghostscript and Redmon
- Here are two web pages that explain how to install the
above tools:
http://masterdev.dyndns.dk/know/freepdf.html
http://stat.tamu.edu/~henrik/GSWriter/GSWriter.html

4) Configure the RedMon printer port used by the PDF virtual printer in
the following mannter:
- Output: "Program handles output"
- The new PDF file should always save to the same file i.e.
C:\temp\output. pdf

Use this for the "Program Arguments" setting:
@c:\gs\pdfconf. txt -sOutputFile="C: \temp\output.pd f" -c .setpdfwrite
-f -

(Note the literal filepath instead of the "%1")

5) Write your own Visual Basic code that prints the report to the pdf
and then uses FileSystemObjec t to copy it to a name/location of your
chosing. Use the WScript.Network object to change the default printer
from your usual default printer to the PDF printer and back again.

Example Code (Access Visual Basic):

Sub PrintReportToPD F(strReport as String, _
strOutputPath as String)

Const PDF_PRINTER as String = "PDF Printer"
Const ORIGINAL_PRINTE R as String = "\\OFFICE\HP132 0"
Const TEMP_PATH as String = "C:\temp\output .pdf"

Dim net as WScript.Network
Dim fso as Scripting.FileS ystemObject

Set net = new WScript.Network
net.setDefaultP rinter PDF_PRINTER

DoCmd.OpenRepor t strReport

Set fso = New Scripting.FileS ystemObject
fso.CopyFile TEMP_PATH, strOutputPath, True
fso.DeleteFile TEMP_PATH
Set fso = Nothing

net.setDefaultP rinter ORIGINAL_PRINTE R
Set net = Nothing

End Sub

For the preceding code to work inside Access 2000, you have to add
references to Microsoft Scripting Runtime and Windows Script Host
Object Model.

My experience so far is that the subroutine DoCmd.OpenRepor t() finishes
outputting the report to PDF very quickly...howev er, if you have a very
large report the output.pdf might be there when the FileSystemObjec t
goes to move/rename it. Therefore, you might want to use the Windows
API Sleep() function.
See < http://support.microsoft.com/kb/q162150/ >

Writing a routine that prints out multiple reports is left as an
exercise to the reader. Personally, I prefer to keep my reports in a
table called "Settings_Repor ts" rather than hardcoding the report names
into my VBA modules.
ADDITIONAL TIPS:

- Use DoCmd.SendObjec t() automate the emailing of the newly-created
reports to managers.

- Use the PDF Toolkit to merge disparate PDF reports into one file
URL: http://www.accesspdf.com/pdftk
This can also be automated using the VBA Shell() function, or in
VBScript via the WScript.Shell.R un() method

- Use VBScript to automate Access, just like Excel or Word.
See the following Microsoft KB article:
ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/kb/q147816/

Code fragment (VBScript):

dim acc
set acc = CreateObject("A ccess.Applicati on")
with acc
..OpenCurrentDa tabase "C:\Reports\Sal es.mdb"

' Call the customer subroutine we defined earlier
..Run("PrintRep ortToPDF", "rptSalesFigure s_Monthly", _
"N:\Marketing\R eports\SalesSum mary" _
& Year(Date()) & Format(Month(Da te()),"00")

[Etc...]

Needless to say, there is a lot that can be done with the approach.
And the best part is that you don't have to learn another API (other
than the standard Windows Script Host and Microsoft Scripting Runtime
libraries, which you really should know anyway). And best of all it's
completely free!
SETTING UP THE PDF PRINTER:
Here are more detailed instructions for installing and configuring the
virtual PDF printer - cribbed from:
< http://masterdev.dyndns.dk/know/freepdf.html >

1. Install Ghostscript to C:\
<http://www.ghostscript .com/doc/AFPL/index.htm>

2. Create a text file (c:\gs\pdfconf. txt) and add the following text:

-Ic:\gs\gs8.11\l ib;c:\gs\fonts
-sDEVICE=pdfwrit e
-dNOPAUSE
-dSAFER

Note that the first line points to version 8.11 of Ghostscript. If
you have a different version or installed it to a different
location, make the appropriate changes.

3. Download, unpack, and Install RedMon (Redirection PortMonitor)
<http://www.cs.wisc.edu/~ghost/redmon/index.htm>
* When you run setup.exe you will only see a message box.

4. Go to Printers/Faxes menu under the Start button in Windows and
add a new printer.
* The driver for the printer must be for a /color postscript/
printer. I chose HP Color Laserjet 4550 PS
* Set the printer port to RPT1: (the redirected port)

5. Configure the port
Redirect Port to: "C:\gs\gs8.11\b in\gswin32c.exe "
(Update this line to reflect your version and location of
Ghostscript, if different)

Arguments for this program are:
@c:\gs\pdfconf. txt -sOutputFile="%1 " -c .setpdfwrite -f -

Output: "Prompt for filename"
Run: "Normal"

6. Try printing something in color to this printer. If everything works
right, you will be prompted for a filename that the PDF file will be
saved under (Don't forget to add the .PDF extension).
'
Hope this helps someone!

Cy******@gmail. com

Nov 13 '05 #1
16 48880
I installed a free program called CutePDF. Its a printer driver, so you can
set it as the default printer for whatever you want to print. Pretty easy, I
think. You might want to check it out. (Oh, I'm not affiliated with the
company, I just think its a nice product, and its free.) -- Mike
Nov 13 '05 #2
I do the same with www.pdf995.com

The VB code switches the default printer to this driver and back when
finished.
"Mike Turco" <mi*******@yaho o-nospam4me.com> wrote in message
news:aQ7Ed.2616 7$Q%4.22751@fed 1read06...
I installed a free program called CutePDF. Its a printer driver, so you can set it as the default printer for whatever you want to print. Pretty easy, I think. You might want to check it out. (Oh, I'm not affiliated with the
company, I just think its a nice product, and its free.) -- Mike

Nov 13 '05 #3
According to their website, you still need to install Ghostscript as the PDF
engine. Previous versions of CutePDF included it, but it is now a separate
install.

Darryl Kerkeslager
"Mike Turco" <mi*******@yaho o-nospam4me.com> wrote:
I installed a free program called CutePDF. Its a printer driver, so you can set it as the default printer for whatever you want to print. Pretty easy, I think. You might want to check it out. (Oh, I'm not affiliated with the
company, I just think its a nice product, and its free.) -- Mike

Nov 13 '05 #4
Tom
Could you please share the code that switches the default printer to this
driver and back when finished.

Thanks!

Tom

"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:kH******** ***********@wag ner.videotron.n et...
I do the same with www.pdf995.com

The VB code switches the default printer to this driver and back when
finished.
"Mike Turco" <mi*******@yaho o-nospam4me.com> wrote in message
news:aQ7Ed.2616 7$Q%4.22751@fed 1read06...
I installed a free program called CutePDF. Its a printer driver, so you can
set it as the default printer for whatever you want to print. Pretty

easy, I
think. You might want to check it out. (Oh, I'm not affiliated with the
company, I just think its a nice product, and its free.) -- Mike


Nov 13 '05 #5
Check http://www.mvps.org/access/reports/rpt0009.htm at "The Access Web", or
what Albert Kallal has at
http://www.members.shaw.ca/AlbertKal.../msaccess.html
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Tom" <ts******@nospa m.please> wrote in message
news:Nw******** ********@newsre ad3.news.pas.ea rthlink.net...
Could you please share the code that switches the default printer to this
driver and back when finished.

Thanks!

Tom

"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:kH******** ***********@wag ner.videotron.n et...
I do the same with www.pdf995.com

The VB code switches the default printer to this driver and back when
finished.
"Mike Turco" <mi*******@yaho o-nospam4me.com> wrote in message
news:aQ7Ed.2616 7$Q%4.22751@fed 1read06...
> I installed a free program called CutePDF. Its a printer driver, so you

can
> set it as the default printer for whatever you want to print. Pretty

easy,
I
> think. You might want to check it out. (Oh, I'm not affiliated with the
> company, I just think its a nice product, and its free.) -- Mike
>
>



Nov 13 '05 #6
Saintor wrote:
I do the same with www.pdf995.com

"Mike Turco" <mi*******@yaho o-nospam4me.com> wrote:
I installed a free program called CutePDF. Its a printer driver, so you can set it as the default printer for whatever you want to print.


I also was using PDF995, which is exactly why I investigated this
solution. However, I wanted to produce my PDFs unattended. When you
have to produce dozens of reports for multiple departments, working
through 40+ "Save As..." dialogs can get a bit tedious (not to mention
error-prone). If you then have to *email* these reports out (or copy
them to shared folders...or whatever) - well, more tedium. Hence,
Ghostscript/Redmon/VBA => Less manual work.

Once you write a VBScript that creates the PDF reports, and use the
Windows Task Scheduler to make the script run overnight, you suddenly
have a lot more time for web surfing ;^)

Someone else also recommended PDFCreator
< http://sector7g.wurzel6.de/pdfcreator/index_en.htm >
However, this program - like CutePDF - uses ghostscript. Why not take a
shortcut and do it yourself?

For those who own a copy of Adobe Acrobat, you can also program the
Acrobat API to create PDFs. However, I believe the approach I posted
is easier, not to mention free.

Nov 13 '05 #7
Tom wrote:
Could you please share the code that switches the default printer to
this driver and back when finished.


One approach is to use multiple Win32 API functions to accomplish this
(ugh...no). Fortunately, Microsoft has generously provided the Windows
Script Host object model. Read about it here:

< http://msdn.microsoft.com/scripting/windowshost >

The object we are interested in is the WshNetwork object aka
"WScript.Networ k" and its handy SetDefaultPrint er method

http://msdn.microsoft.com/library/en...ultprinter.asp

Sample code (VBScript) that prints an Access report to two different
printers

Const REPORTS_MDB = "C:\reports\Sal es.mdb"
Const SALES_REPORT = "rptMonthlySale sSummary"
Const LOCAL_PRINTER = "HP1200"
Const REMOTE_PRINTER = "\\OFFICE\HP132 0"

dim net
dim acc

set acc = CreateObject("A ccess.Applicati on")
acc.OpenCurrent Database(REPORT S_MDB)
set net = CreateObject("W Script.Network" )
net.SetDefaultP rinter(REMOTE_P RINTER)
acc.Docmd.OpenR eport(SALES_REP ORT)
net.SetDefaultP rinter(LOCAL_PR INTER)
acc.DoCmd.OpenR eport(SALES_REP ORT)

[etc.]

(The above code is untested. Read the documentation and code examples
on MSDN and elsewhere before using it.)

I suppose to make this code better, the WScript.Network object should
read the current default printer, switch to the temporary printer for
printing, and then switch back to the original printer. However, I
know my printers in advance and they have been the same for the last 3
years, so I have no problem with hard-coding them into my script.

But what if you have 20 different regional sales managers each with
their own printer? Rather than hardcoding them into my VBScript, I
could also create a table (in Access, or a CSV file, or whatever) that
contains the following fields

Database | Report Name | Printer

The VBScript code would load this table into a recordset and loop
through it to print out the reports.

Nov 13 '05 #8

I also was using PDF995, which is exactly why I investigated this
solution. However, I wanted to produce my PDFs unattended. When you
have to produce dozens of reports for multiple departments, working
through 40+ "Save As..." dialogs can get a bit tedious (not to mention
error-prone).


Well you don't need to.
http://www.freeware995.com/misc/vbacode.txt

Also, since recently, I use the sub 'OutlookSendMes sage' easy to get on
internet to email my PDF reports. All automatic with Windows Task Scheduler
of course.
Nov 13 '05 #9
No offense, but Win32 API programming == yuck. Copy & pasting Win32
API code == double-yuck.

See commandment #8
< http://www.mvps.org/access/tencommandments.htm >

Why puzzle over the inner workings of Windows API functions when
Microsoft has provided WScript.Network and FileSystemObjec t?
Apparently, even *they* don't like Windows API programming, or else
they wouldn't have provided these tools.

Consider the following lines of VB code:

WshNet.SetDefau ltPrinter "PDF Printer"
AccApp.DoCmd.Op enReport "rptSales"
fsObj.CopyFile "C:\temp\output .pdf","N:\marke ting\SalesQ1.pd f",true
WshNet.SetDefau ltPrinter "HP1200"

Even someone who doesn't know about the Windows Script Host or File
System Object (and possibly someone who doesn't know how to program)
can grasp immediately what the above code does. That is its advantage.

Of course, you first have to install Ghostscript and RedMon, and then
set up a virtual PDF printer. This task consists essentially of
un-zipping files onto your hard drive, installing a local printer and
entering a single line of code into the new printer's "Configure Port"
dialog. It takes less than five minutes and no new technical knowledge
(if you code Access databases for a living, you know how to install a
windows printer). From start-to-finish: five minutes. And it's free.

Of course, everyone should find the approach that works best for them.

- CyranoVR

PS - OutlookSendMess age isn't going to work for everyone (my employer
uses Novell Groupwise). The advantage of SendObject is that it works
with any MAPI-compliant email client.

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1316
by: John | last post by:
Hi all, I would like to use the Report building and printing capabilities of the MSAccess DB. I have a ASP.NET page which does calculations based on data in a MSSQL Server and once done will need to have the abiliity to print from IE. The results can be over 1000 rows or greater, therefore using a datagrid, even with paging set to on, is a cumbursome way for the users to print all of the data. Is there a way to create the reports in...
2
4563
by: Sigurd Bruteig | last post by:
Hi all! I have a problem printing multiple reports. The code i use is: Dim stDocName As String stDocName = "rptInvoice" DoCmd.OpenReport stDocName, acNormal, , " = date()" The problem is that invioce details is printed several times on the same invoice and only the first page is shown as first page, even if all reports have only one page. I gess I have to loop trough the code, but I can't figure out how to.
3
10440
by: gudia | last post by:
I want to Programatically generate Access reports in the pdf format using some tool and then email them to people in the same code. Right now I am trying to do this with pdf995 using VBA (emailing is a separate issue, which pdf995 does not addresses). I downloaded pdf995. It controls printing via pdf995.ini file. In the VBA code, I have DoCmd.OpenReport "Report2", acViewNormal. I am having the following problems right now. (a) I do...
11
2464
by: IC_Clearly | last post by:
I'm running Server 2003 and wonder how well WTS works when printing an Access report to a client. Any suggestions would be appreciated. Thanks. IC
5
2863
by: Steven Taylor | last post by:
Hope someone can help. I have an application whereby in order to create one document type I effectively print out 3 or 4 access reports in correct order. So the user goes to the printer, collects the report and has no idea that 3 or 4 reports were actually created in order to produce the printed copy. I now have a need to send that same report to a 'Snapshot' file. Is there a way to combine separate Access reports and have them...
3
1245
by: narayanaRCG | last post by:
Hi, I am printing the reports in VB.Net. It's working fine. But, I wan't to print the current records data only. My report is printing all the data available in it. But, I wan't to print the current data only. For example, from frontend I am storing the student details into backend. and I am preparing report for it. When the next time I give the studetn id it should print only that details with the student id given by me. Can any body tell me...
3
1663
by: charvi | last post by:
can i generate more than 1 excel sheets when a command button is clicked for eg i search for 2 names of audit dept and click print command button where records will be printed in excel again if i search for other 2 names and if i click print command button reports will not be printed.and gives error i have to close the program and load the program again
4
1877
by: MLH | last post by:
How do I ensure my reports are being printed in max resolution of 1200dpi ? That is, can Access control the setting?
0
2333
by: Qtip23 | last post by:
Hello All, So I have a my database information placed on a SharePoint site now that there is an immediate need for a web-based application in my division. I am able to print Access Reports from SharePoint but this annoying non-breaking space mark -- &nbsp -- keeps printing out as well. Is there a way to handle or suppress that from occuring when printing out Access reports? Thanks,
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.