此命令不可用,因为没有文档打开 C# WPF
This command is not available because no document is open C# WPF
我正在从 Word 文档递归生成 XPS 文档,但出现以下错误
错误:
This command is not available because no document is open. at Miscrosoft.office.interop.Word.ApplicationClass.get_ActiveDocument at Line 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
可能是新打开的文件还没有激活。但是 Open
方法 returns 文件。因此,无需激活它或通过索引或名称访问它。
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;
}
在保存之前尝试激活您的文档
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;
}
我正在从 Word 文档递归生成 XPS 文档,但出现以下错误
错误:
This command is not available because no document is open. at Miscrosoft.office.interop.Word.ApplicationClass.get_ActiveDocument at Line 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
可能是新打开的文件还没有激活。但是 Open
方法 returns 文件。因此,无需激活它或通过索引或名称访问它。
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;
}
在保存之前尝试激活您的文档
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;
}