【译文】在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交流群 (群号:483923997) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access窗体导出PDF】...(04.08)
- 【Access窗体导出PDF】...(04.07)
- Access两种方式实现即时更...(03.01)
- Access隐藏与显示lacc...(01.12)
- 【Access高效办公】将每个...(12.23)
- Access21点游戏源代码(12.13)
- 【Access窗体导出Exce...(11.15)
- 【Access开发】Acces...(11.14)
- 通过Access宏录入数据到选...(11.10)
学习心得
最新文章
- 【Access表名称命名建议】将A...(04.20)
- Access学习笔记--用Acce...(04.19)
- 【Access重复项查询示例】将A...(04.17)
- Access快速开发平台企业版--...(04.16)
- 【Access模块示例】通过模块代...(04.15)
- Access查询里面分组合计功能添...(04.13)
- 【Access删除查询】删除数字最...(04.12)
- 显示文件夹中所有文件的修改时间(04.11)
- 铁路工程管理系统;铁路工程管理小程...(04.10)
- 【Access查询示例】怎么将两个...(04.09)