VBA使用Acrobat Distiller生成PDF文件

来源:互联网 发布:mac 远程协助怎么用 编辑:程序博客网 时间:2024/04/29 10:21
原理如下:
1. 使用Acrobat Distiller打印激活worksheet or range生成postscript文件
2. 使用Acrobat Distiller API 将postscript转化成 .PDF 文件
 
首先,在VBA工程中引用Acrobat Distiller
然后,进行Acrobat Distiller的打印设置(必须,否则转化出错!)
将 Do not send fonts to "Adobe PDF" 选项钩选掉(不选)
 
代码:

        Public Sub MakePDF(ByVal strPDFFileName As String)

 

            ' Define the postscript and .pdf file names.

            Dim strPSFileName As String

 

            Dim xlWorksheet As Worksheet

            Dim objPdfDistiller As PdfDistiller

 

            strPSFileName = Left(strPDFFileName, InStrRev(strPDFFileName, "/")) & "tmpPostScript.ps"

 

            ' Print the Excel ActiveSheet to the postscript file

            Set xlWorksheet = ActiveSheet

            Call xlWorksheet.PrintOut(copies:=1, preview:=False, ActivePrinter:="Acrobat Distiller", printtofile:=True, collate:=True, prtofilename:=strPSFileName)

 

            ' Convert the postscript file to .pdf

            Set objPdfDistiller = New PdfDistiller

            Call objPdfDistiller.FileToPDF(strPSFileName, strPDFFileName, "")

 

            ' Finally, delete the postscript file

            Call Kill(strPSFileName)

 

        End Sub


 

程序调用的时候,需要修改Adobe PDF Settings的默认设置:

开始菜单  ⇒  设定  ⇒  打印机 ⇒  选择Adobe PDF,右键选属性 ⇒  选择"详细设定"选项卡 ⇒ 点击"标准设定"按钮 ⇒ 钩选掉"Do no send fonts to 'Adobe PDF'"

 

调用: Sub MakePDFApp() Call MakePDF("c:/firefox.pdf") End Sub 这样就能在指定位置生成指定的PDF文件了。
原创粉丝点击