【译文】在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源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
学习心得
最新文章
- 获取文件修改日期的函数(10.18)
- Access快速开发平台--lis...(10.17)
- Access快速开发平台--普通用...(10.14)
- 【Access源码示例】VBA代码...(10.12)
- Access累乘示例,Access...(10.09)
- 数值8.88,把整数8去掉,转化成...(10.08)
- 用ACCESS开发的销售数据分析软...(10.06)
- 【中秋及国庆优惠】Access培训...(10.05)
- 2024欢度国庆--庆祝国庆华诞7...(10.01)
- 【Access自定义函数】一个繁简...(09.30)