我正在以遞回方式從 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
uj5u.com熱心網友回復:
在保存之前嘗試激活您的檔案
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;
}
uj5u.com熱心網友回復:
可能新打開的檔案尚未激活。但是該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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408094.html
標籤:
