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

发布于 2025-01-05 18:33:42 字数 853 浏览 2 评论 0原文

我正在尝试创建一个在从 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

I'm trying to create a macro that executes when a file is saved from Word (or Excel) 2007+. The macro needs ot check the name/location of the file to decide whether to execute, and then if the filename checks out (probably because it has '_temp' appended to it, or exists in the \temp folder) then as well as saving the file, Word also saves to PDF with the same name but obviously with the .pdf extension instead. Preferably I'd like the PDFing to happen prior to the save, but I'm not fussed. The Word client already has the SaveAsPDForXPS plug-in installed.

So far, I've managed to figure out that I need a macro with FileSave() handler in it, and that (from recording a test macro) the save bit might look like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2025-01-12 18:33:42

好吧,我认为这已经完成了工作,但仍然感激地收到任何指向明显错误的指示。另外,如何将其变成 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

OK, I think that this does the jjob, but any pointers to glaring errors still gratefully received. Also, how to turn it into an Addin is still proving puzzling:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文