【译文】在access中用VBA将WORD文档转化为PDF文件
时 间:2012-05-08 08:37:40
作 者:周芳 ID:24526 城市:上海
摘 要:office2007可以让你轻松的从任何Word文档通过使用Office按钮中的输出菜单键来创建PDF文件 ,(ACCESS通过报表也可以完成),但是最近我不得不为一个客户创建一个程序,因为他想要在ACCESS中就将WORD文档转化为PDF文件,然后通过电子邮件将他们发送出去。
正 文:
原作者:Juan Soto 翻译:周芳
【译文】在ACCESS中用VBA将WORD文档转化为PDF文件
office2007可以让你轻松的从任何Word文档通过使用Office按钮中的输出菜单键来创建PDF文件 ,(ACCESS通过报表也可以完成),但是最近我不得不为一个客户创建一个程序,因为他想要在ACCESS中就将WORD文档转化为PDF文件,然后通过电子邮件将他们发送出去。
注:要使这段代码运行,你将需要在你的Access 数据库中增加一个引用,即Microsoft Word 12.0 Object Library的引用。
Private Sub CreatePDF(strSourceFile As String, strDestFile As String)
Dim objWord As Word.Application
Dim objWordDoc As Word.Document
On Error GoTo ErrorHandler
Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True
Set objWordDoc = objWord.Documents.Open(strSourceFile)
If Not objWord Is Nothing Then
objWordDoc.ExportAsFixedFormat strDestFile, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument
End If
ExitProcedure:
objWordDoc.Close False
objWord.Quit
Set objWordDoc = Nothing
Set objWord = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbInformation, “Error Creating PDF”
End Sub
注意我是在事件处理的一开始就显示Word,不这样做的话,如果你的代码有错,那么可能Word文件就会被“挂”起。
【原文】How to create PDFs in Word using Access VBA
Office 2007 will allow you to easily create PDFs from any Word Document using the output menu on the Office button, (Access does too with Reports), but recently I had to create a procedure for a client that wanted to convert Word docs to PDF, email them and do it all from Access.
Note: To make this code work, you will need to add a reference to Word 2007 to your Access database.
Here is the code:
Private Sub CreatePDF(strSourceFile As String, strDestFile As String)
Dim objWord As Word.Application
Dim objWordDoc As Word.Document
On Error GoTo ErrorHandler
Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True
Set objWordDoc = objWord.Documents.Open(strSourceFile)
If Not objWord Is Nothing Then
objWordDoc.ExportAsFixedFormat strDestFile, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument
End If
ExitProcedure:
objWordDoc.Close False
objWord.Quit
Set objWordDoc = Nothing
Set objWord = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbInformation, “Error Creating PDF”
End Sub
Note how I display Word at the begining of the process, not doing so may leave Word “hanging” in memory if your code comes back with an error.
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access高效办公】按日期...(06.12)
- 合并列数据到一个文本框的示例;...(05.06)
- 通过命令按钮让Access列表...(04.24)
- 统计当月之前(不含当月)的记录...(03.11)
- 【Access Inputbo...(03.03)
- 按回车键后光标移动到下一条记录...(02.12)
- 【Access Dsum示例】...(02.07)
- Access对子窗体的数据进行...(02.05)
- 【Access高效办公】上月累...(01.09)

学习心得
最新文章
- Access快速开发平台--Acc...(06.16)
- 【Access高效办公】按日期区间...(06.12)
- Access快速开发平台--生成复...(06.07)
- 仓库管理实战课程(25)-导航菜单...(06.06)
- 仓库管理实战课程(24)-库存月结...(06.05)
- 分享一个简单的X氏家谱小示例(06.04)
- 仓库管理实战课程(23)-先入先出(06.02)
- 仓库管理实战课程(22)-代理商库...(05.30)
- 仓库管理实战课程(21)-安全库存...(05.28)
- Access快速开发平台--如何添...(05.26)