【问题标题】:This command is not available because no document is open C# WPF此命令不可用,因为没有打开文档 C# WPF
【发布时间】:2022-01-04 21:46:45
【问题描述】:

我正在以递归方式从 Word 文档生成 XPS 文档,但出现以下错误

错误:

此命令不可用,因为没有打开文档。在第 65 行的 Miscrosoft.office.interop.Word.ApplicationClass.get_ActiveDo‌​cument

这是:

wordApp.ActiveDocument.SaveAs2(xpsFile, FileFormat: Word.WdSaveFormat.wdFormatXPS);

我正在使用以下代码将 Word 文件转换为 XPS 文件

public static string convertWordToXps(string path, string wordDocName)
{
    Word.Application wordApp = new Word.Application();
    wordApp.Documents.Open(string.Concat(path, "\\", wordDocName), ConfirmConversions: false, ReadOnly: false);

    string xpsFile = string.Concat(path, "\\", Path.GetFileNameWithoutExtension(wordDocName), ".xps");

    try
    {
        //wordApp.ActiveDocument.ExportAsFixedFormat(xpsFileName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument, 1, 1, WdExportItem.wdExportDocumentContent, false, true, WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, true, false, nullObject);
        wordApp.ActiveDocument.SaveAs2(xpsFile, FileFormat: Word.WdSaveFormat.wdFormatXPS);
        return xpsFile;
    }
    catch (Exception e)
    {
        MessageBox.Show(e.getDetailedErrorMessage());
    }
    finally
    {
        wordApp.Quit(SaveChanges: false, OriginalFormat: Type.Missing, RouteDocument: Type.Missing);
    }
    return null;
}

搜索功能

private void SearchDocuments(string directoryPath)
{
    try
    {
        foreach (string fullName in Directory.GetFiles(directoryPath, "*.odt"))
        {
            InstructionsViewModel.convertWordToXps(System.IO.Path.GetDirectoryName(fullName), System.IO.Path.GetFileNameWithoutExtension(fullName));
        }
        foreach (string nestedDirectory in Directory.GetDirectories(directoryPath))
        {
            SearchDocuments(nestedDirectory);
        }
    }
    catch (System.Exception error)
    {

    }
}

我想将所有文件夹中的所有word文件都转换为XPS

【问题讨论】:

  • 旁注:使用Path.Combine Method 代替字符串连接。它负责删除多余的反斜杠或添加缺少的反斜杠。
  • @OlivierJacot-Descombes 感谢您的建议,但这不是这里的问题
  • @Muhammadzubair 在保存之前尝试激活您的文档:wordApp.Documents(wordDocName"+".extension_of_doc").Activate() 您只打开了文档,但它没有激活。
  • @BartoszOlchowik 你能发布你的解决方案吗?

标签: c# wpf ms-word word xps


【解决方案1】:

在保存之前尝试激活您的文档

wordApplication= new Microsoft.Office.Interop.Word.Application();
var document= wordApplication.Documents.Open(@"path/to/document.docx");  
document.Activate();
// and now save.

在您的代码中,它看起来像:

public static string convertWordToXps(string path, string wordDocName)
{
    Word.Application wordApp = new Word.Application();
    var document= wordApp.Documents.Open(string.Concat(path, "\\", wordDocName), ConfirmConversions: false, ReadOnly: false);
    document.Activate();
    string xpsFile = string.Concat(path, "\\", Path.GetFileNameWithoutExtension(wordDocName), ".xps");

    try
    {
        //wordApp.ActiveDocument.ExportAsFixedFormat(xpsFileName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument, 1, 1, WdExportItem.wdExportDocumentContent, false, true, WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, true, false, nullObject);
        wordApp.ActiveDocument.SaveAs2(xpsFile, FileFormat: Word.WdSaveFormat.wdFormatXPS);
        return xpsFile;
    }
    catch (Exception e)
    {
        MessageBox.Show(e.getDetailedErrorMessage());
    }
    finally
    {
        wordApp.Quit(SaveChanges: false, OriginalFormat: Type.Missing, RouteDocument: Type.Missing);
    }
    return null;
}

【讨论】:

  • 不可调用成员'_Applcation.Documents'不能像方法一样使用
  • @Muhammadzubair 立即尝试。这是因为Documents 是一个列表,您必须找到要激活的特定文档。通过给出名称或索引。更新了我的代码,请重试。
  • 其实我不想只转换一个文件。我想转换我提供的路径中所有文件夹内的所有“.odt”文件。路径是这样的:Z:\Instructions。并且内部说明还有其他文件夹,并且在每个文件夹中都有“.odt”文件,我想将其转换为 XPS 上面的行我改变了,没有错误,但它没有转换我不知道为什么
  • @Muhammadzubair:好的,我编辑了我的答案。您可以复制并粘贴该代码而不是您的函数,它应该适用于任何文档。
  • 对象引用未设置为对象的实例。'文档为空。我复制粘贴你的功能,我得到了上面的错误
【解决方案2】:

可能新打开的文档尚未激活。但是Open 方法会返回文档。因此,无需激活它或通过索引或名称访问它。

Word.Document doc = wordApp.Documents.Open(...);
doc.SaveAs2(...);

整个方法

public static string convertWordToXps(string path, string wordDocName)
{
    var wordApp = new Word.Application();
    Word.Document doc = wordApp.Documents.Open(Path.Combine(path, wordDocName), ConfirmConversions: false, ReadOnly: false);

    string xpsFile = Path.Combine(path, Path.ChangeExtension(wordDocName, ".xps"));

    try {
        doc.SaveAs2(xpsFile, FileFormat: Word.WdSaveFormat.wdFormatXPS);
        return xpsFile;
    } catch (Exception e) {
        MessageBox.Show(e.getDetailedErrorMessage());
    } finally {
        wordApp.Quit(SaveChanges: false, OriginalFormat: Type.Missing, RouteDocument: Type.Missing);
    }
    return null;
}

【讨论】:

  • 有点混乱。我必须将什么传递给 doc.SaveAs2(...); ???在哪里?
  • 你已经在你的代码中做到了。我并不是要更改代码的整体结构,只是建议更改两行代码。在 (...) 中填写您已有的内容。
  • 对象引用未设置为对象的实例。' :( 不工作
  • 我不能debug你的代码。如果doc 为空,则可能无法打开文档。
  • 是的,我做的完全一样,是的,文档为空。
猜你喜欢
  • 2015-03-04
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 2018-03-19
  • 2019-12-29
  • 1970-01-01
相关资源
最近更新 更多