HOWTO:自动将访问报告打印到PDF文件 [英] HOWTO: Automate Printing of Access Reports to PDF files

查看:123
本文介绍了HOWTO:自动将访问报告打印到PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来自动打印Microsoft Access的方法

报告为PDF格式,即无人值守且无烦恼的另存为...

对话框,更重要的是 - 无需使用商业

计划,如Adobe Acrobat及其相关API。


该技术使用Ghostscript和Redirection Port Monitor - 用于创建由Russell Lang免费提供的PDF文档的两个免费

程序。

实际自动化需要使用FileSystemObject进行VBA编码,
WScript.Network(Windows脚本宿主网络对象)和访问

自动化。

说明:


1)安装Ghostscript

- Postscript语言的解释器和PDF
http://www.cs.wisc.edu/~ghost/


2)安装RedMon - 重定向端口监视器

- 将特殊打印机端口重定向到程序(例如

Ghostscript) http://www.cs.wisc.edu/~ghost/ redmon /


3)使用Ghostscript和Redmon设置虚拟PDF打印机

- 这是两个网页,解释如何安装

以上工具:
http:// masterdev.dyndns.dk/know /freepdf.html
http:/ /stat.tamu.edu/~henrik/GSWriter/GSWriter.html


4)配置PDF虚拟打印机使用的RedMon打印机端口

以下管理:

- 输出:程序处理输出

- 新的PDF文件应始终保存到同一个文件,即

C:\ temp \output.pdf


用于程序参数和程序参数。设置:

@c:\gs\pdfconf.txt -sOutputFile =" C:\ temp \ output.pdf" -c .setpdfwrite

-f -


(注意文字文件路径而不是%1)


5)编写自己的Visual Basic代码,将报告打印到pdf

,然后使用FileSystemObject将其复制到

选择的名称/位置。使用WScript.Network对象将默认打印机

从常用的默认打印机更改为PDF打印机,然后再返回。


示例代码(Access Visual Basic ):


Sub PrintReportToPDF(strReport as String,_

strOutputPath as String)


Const PDF_PRINTER as String =" PDF打印机>

Const ORIGINAL_PRINTER为String =" \\OFFICE\HP1320"

Const TEMP_PATH as String =" C:\ temp \\\put.pdf"


Dim net as WScript.Network

Dim fso as Scripting.FileSystemObject


设置net = new WScript.Network

net.setDefaultPrinter PDF_PRINTER


DoCmd.OpenReport strReport


设置fso =新的Scripting.FileSystemObject

fso.CopyFile TEMP_PATH,strOutputPath,True

fso.DeleteFile TEMP_PATH

设置fso = Nothing


net.setDefaultPrinter ORIGINAL_PRINTER

设置net = Nothing


End Sub


要使上述代码在Access 2000中运行,你必须添加

引用Microsoft脚本运行时和Windows脚本宿主

对象模型。


到目前为止我的经验是子程序DoCmd .OpenReport()完成

非常快速地将报告输出到PDF ...但是,如果你有一个非常好的
大报告,那么当FileSystemObject时,output.pdf可能就在那里

去移动/重命名它。因此,您可能希望使用Windows

API Sleep()函数。

请参阅< http://support.microsoft.com/kb/q162150/ > ;


编写一个打印出多个报告的例程留给读者一个

练习。就个人而言,我更喜欢将报告保存在名为Settings_Reports的

表中。而不是将报告名称

硬编码到我的VBA模块中。

附加提示:


- 使用DoCmd.SendObject()自动化通过电子邮件发送新创建的

报告给管理员。


- 使用PDF工具包将不同的PDF报告合并到一个文件中

网址: http://www.accesspdf.com/pdftk

这也可以使用VBA Shell()函数自动完成,或者通过WScript.Shell.Run()方法在

VBScript中自动化


- 使用VBScript自动化Access,就像Excel或Word一样。

请参阅以下Microsoft知识库文章:

ACC:使用Microsoft Access作为自动化服务器
http://support.microsoft.com/kb/q147816/


代码片段(VBScript):


dim acc

set acc = CreateObject(" Access.A pplication")

with acc

..OpenCurrentDatabase" C:\ _Reports\Sales.mdb"


' '调用我们之前定义的客户子程序

..运行(PrintReportToPDF,rptSalesFigures_Monthly,_

" N:\ Marketing\Reports \\ \\SalesSummary" _

&年(日期())&格式(月(日期()),00)


[等等]


不用说,有这个方法可以做很多事情。

最好的部分是你不必学习另一个API(其他比标准Windows脚本宿主更多的b $ b)和Microsoft Scripting Runtime

库,你真的应该知道它们。最重要的是它是完全免费的



设置PDF打印机:

以下是有关安装和配置的更详细说明

虚拟PDF打印机 - 来自:
< http://masterdev.dyndns.dk/know/freepdf.html>


1.将Ghostscript安装到C:\

< http://www.ghostscript.com/doc/AFPL/index。 htm>


2.创建一个文本文件(c:\\\pdfconf.txt)并添加以下文字:


-Ic:\\\\gs8.11 \ lib; c:\\\\\ _s>

-sDEVICE = pdfwrite

-dNOPAUSE

-dSAFER


请注意,第一行指向Ghostscript的版本8.11。如果

您有不同的版本或将其安装到不同的

位置,请进行相应的更改。


3.下载,解包和安装RedMon(Redirection PortMonitor)

< http://www.cs.wisc.edu/~ghost/redmon/index.htm>

*当您运行setup.exe时,您将只看到一个消息框。


4.转到Windows中开始按钮下的打印机/传真菜单,然后再添加
一台新的打印机。

*打印机的驱动程序必须是/ color postscript /
打印机。我选择HP Color Laserjet 4550 PS

*将打印机端口设置为RPT1 :(重定向端口)


5.配置端口

将端口重定向到:C:\gs\gs8.11\bin\gswin32c.exe

(更新此行以反映您的版本和位置
Ghostscript,如果不同的话)


这个程序的参数是:

@c:\gs\pdfconf.txt -sOutputFile = "%1" -c .setpdfwrite -f -


输出:提示文件名

运行:" Normal"


6.尝试向本打印机打印彩色内容。如果一切正常工作

正确,系统将提示您输入PDF文件将保存的文件名(不要忘记添加.PDF扩展名)。

''

希望这有助于某人!

Cy ****** @ gmail.com

解决方案

我安装了一个名为CutePDF的免费程序。它是一个打印机驱动程序,因此您可以将它设置为默认打印机,无论您想要打印什么。很简单,我想b $ b。你可能想看一下。 (哦,我不是

公司的附属公司,我认为它是一个不错的产品,而且是免费的。) - Mike


< blockquote>我对 www.pdf995.com 做同样的事情


VB代码将默认打印机切换到此驱动程序,并在

完成后返回。

Mike Turco < MI ******* @ yahoo-nospam4me.com>写在消息

news:aQ7Ed.26167


Q%4.22751@fed1read06 ...

我安装了一个名为CutePDF的免费程序。它是一个打印机驱动程序,所以你
可以将它设置为任何你想要打印的默认打印机。很简单,我认为是
。你可能想看一下。 (哦,我与
公司没有关联,我认为它是一个很好的产品,而且它是免费的。) - Mike



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 FileSystemObject,
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.pdf" -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 FileSystemObject 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 PrintReportToPDF(strReport as String, _
strOutputPath as String)

Const PDF_PRINTER as String = "PDF Printer"
Const ORIGINAL_PRINTER as String = "\\OFFICE\HP1320"
Const TEMP_PATH as String = "C:\temp\output.pdf"

Dim net as WScript.Network
Dim fso as Scripting.FileSystemObject

Set net = new WScript.Network
net.setDefaultPrinter PDF_PRINTER

DoCmd.OpenReport strReport

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

net.setDefaultPrinter ORIGINAL_PRINTER
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.OpenReport() finishes
outputting the report to PDF very quickly...however, if you have a very
large report the output.pdf might be there when the FileSystemObject
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_Reports" rather than hardcoding the report names
into my VBA modules.
ADDITIONAL TIPS:

- Use DoCmd.SendObject() 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.Run() 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("Access.Application")
with acc
..OpenCurrentDatabase "C:\Reports\Sales.mdb"

'' Call the customer subroutine we defined earlier
..Run("PrintReportToPDF", "rptSalesFigures_Monthly", _
"N:\Marketing\Reports\SalesSummary" _
& Year(Date()) & Format(Month(Date()),"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\lib;c:\gs\fonts
-sDEVICE=pdfwrite
-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\bin\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

解决方案

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


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*******@yahoo-nospam4me.com> wrote in message
news:aQ7Ed.26167


Q%4.22751@fed1read06...

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



这篇关于HOWTO:自动将访问报告打印到PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆