【问题标题】:VBA - Save Word doc as pdf from Excel - Named argument not foundVBA - 从 Excel 将 Word 文档另存为 pdf - 未找到命名参数
【发布时间】:2014-09-17 11:20:59
【问题描述】:

我正在尝试让 Excel 脚本创建一个文档文件并将其保存为 pdf 文件。创建文档没有问题,但找到正确的代码将其保存为 pdf 文件似乎是一个问题。我搜索了互联网,有很多手册和相同问题的答案都归结为相同的代码:

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
ActiveDocument.Path & "\" & ActiveDocument.Name & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False

不知何故,这似乎对我不起作用,因为它给了我错误“未找到命名参数”。这是我的脚本(我省略了不相关的行,我确定路径和文件名存储正确)。

'Dim Word formfield values
Dim objWord As Object
Dim strDocName As String, strDocName1 As String, strDocName2 As String
Dim strMyPath As String

'Declare Word variables
Set objWord = CreateObject("word.application")
objWord.Visible = True

With objWord.activedocument
    'fill in a bunch of formfields

    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strMyPath & "\" & strDocName2, _
    OpenAfterExport:=False
    .Close
End With

我在这里做错了什么?

【问题讨论】:

    标签: excel vba pdf ms-word


    【解决方案1】:

    它不起作用的原因是 Excel 不知道 Word 的任何常量值是什么,例如 wdExportAllDocument 或 wdExportCreateNoBookmarks。

    为了使该代码正常工作,您必须在项目中添加一个引用,方法是单击“工具”--->“引用...”,然后单击旁边的复选框“Microsoft Word 14.0 对象库”。然后,您可以使用您在问题顶部发布的代码的清理版本。请记住,像 ActiveDocument 这样的全局变量在 Excel 中不存在,您必须改用 objWord.ActiveDocument。另外,对于您的情况,我建议您将代码切换为使用 Early Binding(有关差异的描述,请参见 here),以便您拥有智能感知。这可以防止因拼写错误而引起的麻烦。

    【讨论】:

      【解决方案2】:

      谢谢黑鹰!

      与此同时,我找到了另一种解决方案。我需要另一个 Object (objWordDoc) 变量,我已将“With objWord.activedocument”替换为:

      Set objWordDoc = objWord.Documents.Add(strDocToOpen)
      
      With objWordDoc
      

      不过,您的建议将来一定会派上用场的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-31
        • 2020-04-26
        • 2018-11-14
        • 1970-01-01
        相关资源
        最近更新 更多