Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > 综合其它

【译文】在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源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助