Word宏在文件保存时保存为PDF

时间:2012-02-16 09:58:26

标签: excel vba ms-word pdf-generation

我正在尝试创建一个从Word(或Excel)2007+保存文件时执行的宏。宏需要检查文件的名称/位置以决定是否执行,然后如果文件名检出(可能是因为它附加了'_temp',或存在于\ temp文件夹中),那么以及保存在文件中,Word也保存为具有相同名称的PDF,但显然使用.pdf扩展名。我希望PDF文件能够在保存之前发生,但我并不感到困惑。 Word客户端已经安装了SaveAsPDForXPS插件。

到目前为止,我已经设法弄清楚我需要一个带有FileSave()处理程序的宏,并且(从记录测试宏)保存位可能如下所示:

Sub FileSave()
'
' FileSave Macro
'
'
  ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\Documents and Settings\rdyce\Desktop\Doc1.pdf", ExportFormat:= _
        wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False


End Sub

1 个答案:

答案 0 :(得分:2)

好吧,我认为这可以胜任这项工作,但任何指向明显错误的指针仍然感激不尽。此外,如何将其变成Addin仍然令人费解:

Sub FileSave()
'
' FileSave Macro
'

    ActiveDocument.Save

    Dim StrFile As String
    Dim StrPath As String
    Dim StrName As String
    Dim StrPDFName As String

    StrPath = ActiveDocument.Path 'Get document path
    StrFile = ActiveDocument.Name 'Get document name

    If InStr(StrFile, "_tempkey") Then 'Check if this is a special file

        If InStr(StrFile, ".") Then 
            StrName = Left(StrFile, (InStr(StrFile, ".") - 1))
        Else
            StrName = StrFile
        End If

        StrPDFName = StrPath + "\" + StrName + ".pdf"

        ActiveDocument.ExportAsFixedFormat OutputFileName:=StrPDFName, _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
            OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False

    End If

End Sub