如何利用excel VBA生成PDF文件?
首先,加载引用,如图
然后,在打印机中对Acrobat Distiller的打印进行设置设置(必须,否则转化出错!)
Public Sub MakePDF(ByVal strPDFFileName As String)
Dim strPSFileName As String
Dim xlWorksheet As Worksheet
Dim objPdfDistiller As PdfDistiller
strPSFileName = Left(strPDFFileName, InStrRev(strPDFFileName, "/")) & "tmpPostScript.ps"
Set xlWorksheet = ActiveSheet
Call xlWorksheet.PrintOut(copies:=1, preview:=False, ActivePrinter:="Acrobat Distiller", printtofile:=True, collate:=True, prtofilename:=strPSFileName)
Set objPdfDistiller = New PdfDistiller
Call objPdfDistiller.FileToPDF(strPSFileName, strPDFFileName, "")
Call Kill(strPSFileName)
End Sub
使用以下程序:
Sub test()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Documents and Settings\Administrator\桌面\test.pdf"
End Sub
这一个程序就够了 就是这么简单
PDF默认保存位置为桌面,名字叫TEST,名字路径你可以直接改下。
Sub PDF()
ActiveSheet.ExportAsFixedFormat xlTypePDF, "D:" & "" & ActiveSheet.Name & "-" & Format(Now, "yyyymmddhhmmss")
End Sub
- 复制代码