【译文】在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列表...(04.24)
- 统计当月之前(不含当月)的记录...(03.11)
- 【Access Inputbo...(03.03)
- 按回车键后光标移动到下一条记录...(02.12)
- 【Access Dsum示例】...(02.07)
- Access对子窗体的数据进行...(02.05)
- 【Access高效办公】上月累...(01.09)
- 【Access高效办公】上月累...(01.06)
- 【Access Inputbo...(12.23)

学习心得
最新文章
- 仓库管理实战课程(15)-月度库存...(04.30)
- Access选择打印机、横纵向、纸...(04.29)
- 仓库管理实战课程(14)-出库功能...(04.26)
- 通过命令按钮让Access列表框指...(04.24)
- 仓库管理实战课程(13)-入库功能...(04.21)
- Access控件美化之--美化按钮...(04.19)
- Access多行文本按指定字符筛选...(04.18)
- Microsoft Access数...(04.18)
- 仓库管理实战课程(12)-月度结存...(04.16)
- 仓库管理实战课程(11)-人性化操...(04.15)