I am Generating XPS document from Word document recursively but I am getting Error the Error below
Error:
This command is not available because no document is open. at Miscrosoft.office.interop.Word.ApplicationClass.get_ActiveDocument at Line 65
which is:
wordApp.ActiveDocument.SaveAs2(xpsFile, FileFormat: Word.WdSaveFormat.wdFormatXPS);
I am using the Following Code to covert the Word Files to XPS files
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;
}
Search Function
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)
{
}
}
i want to covert all the word files to XPS inside the all folders
CodePudding user response:
Before saving try to activate your document
wordApplication= new Microsoft.Office.Interop.Word.Application();
var document= wordApplication.Documents.Open(@"path/to/document.docx");
document.Activate();
// and now save.
In your code it would look like:
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;
}
CodePudding user response:
Probably the newly opened document is not yet active. But the Open method returns the document. So, no need to activate it or access it through an index or name.
Word.Document doc = wordApp.Documents.Open(...);
doc.SaveAs2(...);
The whole method
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;
}
