I am Generating XPS document from Word document recursively but I am getting 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 convert 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 convert all the Word files to XPS inside all the folders.
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;
}