此命令不可用,因为没有打开文档C# WPF

tct7dpnv  于 2022-11-18  发布在  C#
关注(0)|答案(2)|浏览(431)

我从Word文档递归生成XPS文档,但我得到下面的错误:

错误:

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

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。

6yt4nkrj

6yt4nkrj1#

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

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;
}
mnemlml8

mnemlml82#

可能新打开的文档还没有激活,但是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;
}

相关问题