对 SaveCopyAs 的 Word Interop 调用抛出 COMException“该方法在该对象上不可用。”

问题描述 投票:0回答:0

在 Office 2013 主互操作程序集中,Word 互操作程序集有一个方法SaveCopyAs
(Office 2010 PIA 中不存在此方法。)

我需要在 C# VSTO Word 加载项中调用此方法,但它因 COMException“该方法在该对象上不可用”而失败。 (HRESULT = 0x800A1704)。

我已经在安装了 Word 2013 的 PC 上和另一 (2) 台安装了 Word for Microsoft 365 的 PC 上进行了尝试,结果相同。

上下文:VSTO 加载项在 Word 功能区上有一个按钮,该按钮的 onAction 代码将当前文档的副本保存到我们的文档存储中。如果当前 Word 文档是从文件夹中打开的,或者已经保存,我不想使用另存为,因为这会改变当前文档的路径并破坏后续的保存。

// (Add a reference to Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c)
var wordApplication = (Microsoft.Office.Interop.Word.Application)Marshal.GetActiveObject("Word.Application");
var document = wordApplication.ActiveDocument;
//document.GetType().Assembly.Dump(); // LINQPad
//System.Diagnostics.FileVersionInfo.GetVersionInfo(document.GetType().Assembly.Location).Dump(); // LINQPad
if (string.IsNullOrEmpty(document.Path))
    document.SaveAs("Document must be saved first.docx");
document.SaveCopyAs("Document SaveCopyAs.docx"); // <- this throws

(如果版本很重要)在失败时,document.GetType().Assembly 是 Microsoft.Office.Interop.Word.dll (Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken =71e9bce111e9429c) 并具有 FileVersion=15.0.4603.1000(作为 Microsoft Office 365 的一部分安装)。

例外情况:

Message: That method is not available on that object. 
InnerException: null 
TargetSite.RuntimeMethodInfo: DocumentClass.SaveCopyAs (Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePasswor... 
StackTrace: 
   at Microsoft.Office.Interop.Word.DocumentClass.SaveCopyAs(Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks, Object& CompatibilityMode)
   at UserQuery 
HelpLink: wdmain11.chm#25372 
Source: Microsoft Word 
HResult: -2146822396 
ms-word com vsto office-interop
© www.soinside.com 2019 - 2024. All rights reserved.