Erwin Alcantara

Erwin Alcantara

  • NA
  • 18
  • 1.5k

How can I unload xps file in document viewer

Sep 26 2019 1:12 PM
I have a word doc/rtf file and got some code that it will convert to it xps doc and It can be loaded in the DocumentViewer, I have a button that will open the file in ms word so it can be edited and saved. The problem is since the xps file is open in documentviewer, I was not able to reload the xps document in document viewer. Can some help me unloading file in document viewer. Below is my code.
  1. private XpsDocument ConvertWordDocToXPSDoc(string wordDocName, string xpsDocName) {  
  2.  // Create a WordApplication and add Document to it  
  3.  Microsoft.Office.Interop.Word.Application  
  4.  wordApplication = new Microsoft.Office.Interop.Word.Application();  
  5.  wordApplication.Documents.Add(wordDocName);  
  6.  Document doc = wordApplication.ActiveDocument;  
  7.  try {  
  8.   doc.SaveAs(xpsDocName, WdSaveFormat.wdFormatXPS);  
  9.   wordApplication.Quit();  
  10.   XpsDocument xpsDoc = new XpsDocument(xpsDocName, FileAccess.ReadWrite);  
  11.   return xpsDoc;  
  12.  } catch (Exception exp) {  
  13.   string str = exp.Message;  
  14.  }  
  15.  return null;  
  16. }  
  17. private void LoadMyreminder() {  
  18.  string XPSMyReminder = String.Concat(Path.GetDirectoryName(_myReminder), "\\",  
  19.   Path.GetFileNameWithoutExtension(_myReminder), ".xps");  
  20.  myRemviewer.Document = ConvertWordDocToXPSDoc(  
  21.   _myReminder,  
  22.   XPSMyReminder).GetFixedDocumentSequence();  
  23. }  
  24. private void BtnEditMyrem_Click(object sender, RoutedEventArgs e) {  
  25.  Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application {  
  26.   Visible = true  
  27.  };  
  28.  Document document = ap.Documents.Open(_myReminder);  
  29. }