此命令不可用,因为在获取活动文档时没有打开任何文档

This command is not available because no document is open while getting Active document

try
{
     Microsoft.Office.Interop.Word.Application WordObj = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
     Office.CustomXMLParts currClassification = WordObj.ActiveDocument.CustomXMLParts;
}
catch(Exception ex)
{
     //I am getting, This command is not available because no document is open. this error here.
}

当我使用上面的代码时,出现了这个错误:

This command is not available because no document is open.

此致

实际上,您在 word 应用程序中没有打开文档时试图访问活动文档,因此出现错误。 您的 word 应用程序已打开,但其中没有打开文档,即您在 word 应用程序的主屏幕,如图所示。

尝试使用以下代码检查您的应用程序中是否有任何打开的文档,然后访问ActiveDocument

if(WordObj.Documents.Count >= 1)