VBA - 将 Word 文档从 Excel 保存为 pdf - 未找到命名参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25889172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:34:19  来源:igfitidea点击:

VBA - Save Word doc as pdf from Excel - Named argument not found

excelvbapdfms-word

提问by RobK

I'm trying to let an Excel script create a document file and save it as pdf file. Creating the document goes without a problem, but finding the right code to save it as pdf file seems to be a problem. I've searched the internet and there are many manuals and answers to the same question which all come down to the same code:

我正在尝试让 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

Somehow that doesn't seem to do the trick for me, as it gives me the error "Named argument not found". Here's my script (I've left out the irrelevant lines and I'm sure the path and filename are stored correctly).

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

'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

What am I doing wrong here?

我在这里做错了什么?

回答by Blackhawk

The reason it doesn't work is that Excel doesn't know what any of the constant values of Word are, like wdExportAllDocumentor wdExportCreateNoBookmarks.

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

In order to make that code work, you would have to add a referencein your project by clicking "Tools" ---> "References..." and then clicking the checkbox next to "Microsoft Word 14.0 Object Library". You can then use a cleaned up version of the code that you posted at the top of your question. Keep in mind globals like ActiveDocumentdon't exist in Excel and you will have to use objWord.ActiveDocumentinstead. Also, for your case I would suggest switching your code to use Early Binding (see herefor a description of the difference) so that you will have intellisense. This can prevent troublesome errors caused by misspelling.

为了使该代码工作,您必须通过单击“工具”--->“引用...”,然后单击“Microsoft Word 14.0 对象库”旁边的复选框在项目中添加引用。然后,您可以使用您在问题顶部发布的代码的清理版本。请记住,像ActiveDocumentExcel 中不存在这样的全局变量,您将不得不使用它objWord.ActiveDocument。另外,对于您的情况,我建议您将代码切换为使用早期绑定(请参阅此处了解差异的描述),以便您拥有智能感知。这可以防止由拼写错误引起的麻烦错误。

回答by RobK

Thanks Blackhawk!

感谢黑鹰!

In the meantime I've found another solution. I needed another Object (objWordDoc) variable and I've replaced "With objWord.activedocument" with:

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

Set objWordDoc = objWord.Documents.Add(strDocToOpen)

With objWordDoc

Nevertheless your suggestions will surely come in handy in the future!

尽管如此,您的建议将来肯定会派上用场!