diff --git a/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs b/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs index 9267e2cb..5288803e 100644 --- a/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs +++ b/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs @@ -169,67 +169,96 @@ namespace VEPROMS.CSLA.Library // check the Word file extension that is saved in the tblDocuments SQL database table DocumentInfo docInfo = itmInfo.MyContent.MyEntry.MyDocument; if (docInfo.FileExtension.ToUpper() == ".DOCX") return; // already a DOCX - no need to convert + FrmPopupStatusMessage pmsg = null; + DSOFile myfile = null; + LBWordLibrary.LBApplicationClass ap = null; + LBWordLibrary.LBDocumentClass doc = null; + string orgFilename = null; + string filename = null; + FileInfo fi = null; + FileStream fs = null; + SectionInfo msi = null; + Section sec = null; + SectionConfig cfg = null; - // show user a status window of the Word section being converted to DOCX - // use the section number (DisplayNumber) unless the length is zero, then use the section title (DisplayText) - string statMsg = itmInfo.DisplayNumber; - if (statMsg.Length == 0) - statMsg = itmInfo.DisplayText; - FrmPopupStatusMessage pmsg = new FrmPopupStatusMessage("Converting This Section to Word DOCX", statMsg); - pmsg.Show(); + try + { + // show user a status window of the Word section being converted to DOCX + // use the section number (DisplayNumber) unless the length is zero, then use the section title (DisplayText) + string statMsg = itmInfo.DisplayNumber; + if (statMsg.Length == 0) + statMsg = itmInfo.DisplayText; + pmsg = new FrmPopupStatusMessage("Converting This Section to Word DOCX", statMsg); + pmsg.Show(); - // Get Document as file - it's placed in the user's temporary folder (like we do when we edit a Word section) - DSOFile myfile = new DSOFile(docInfo); + // Get Document as file - it's placed in the user's temporary folder (like we do when we edit a Word section) + myfile = new DSOFile(docInfo); - // Open MSWord App - LBWordLibrary.LBApplicationClass ap = new LBWordLibrary.LBApplicationClass(); - LBWordLibrary.LBDocumentClass doc = ap.Documents.Open(myfile.FullName); + // Open MSWord App + ap = new LBWordLibrary.LBApplicationClass(); + doc = ap.Documents.Open(myfile.FullName); - // Older versions of PROMS saved the Word section as either a RTF or the old-style Word DOC file - // In either case, we want to convert it to a Word DOCX file - // So now create a file name with the .DOCX extension - string orgFilename = myfile.FullName.ToUpper(); - string filename = orgFilename.Replace(".RTF", ".DOCX").Replace(".DOC", ".DOCX"); // we want to convert either .RTF or .DOC Word sections + // Older versions of PROMS saved the Word section as either a RTF or the old-style Word DOC file + // In either case, we want to convert it to a Word DOCX file + // So now create a file name with the .DOCX extension + orgFilename = myfile.FullName.ToUpper(); + filename = orgFilename.Replace(".RTF", ".DOCX").Replace(".DOC", ".DOCX"); // we want to convert either .RTF or .DOC Word sections - // This calls Word's convert function to convert the opened .DOC or .RTF to DOCX and save it to our new DOCX file name - doc.SaveAs(filename, LBWordLibrary.LBWdSaveFormat.wdFormatXMLDocument); // Convert to Word DOCX - doc.Close(); - ap.Quit(); // close the Word app + // This calls Word's convert function to convert the opened .DOC or .RTF to DOCX and save it to our new DOCX file name + doc.SaveAs(filename, LBWordLibrary.LBWdSaveFormat.wdFormatXMLDocument); // Convert to Word DOCX + doc.Close(); + doc = null; - // Now read in the new .DOCX file and save the contents to the SQL database - FileInfo fi = new FileInfo(filename); - FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);// B2016-053 - long len = fs.Length; - byte[] ByteArray = new byte[len]; - int nBytesRead = fs.Read(ByteArray, 0, (int)len); - fs.Close(); + // Now read in the new .DOCX file and save the contents to the SQL database + fi = new FileInfo(filename); + fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);// B2016-053 + long len = fs.Length; + byte[] ByteArray = new byte[len]; + int nBytesRead = fs.Read(ByteArray, 0, (int)len); - bool isLibraryDocument = (docInfo.LibTitle != null && docInfo.LibTitle != ""); - Document myDoc = null; - if (isLibraryDocument) - myDoc = Document.Get(docInfo.DocID); - else - myDoc = Document.MakeDocument(null, ByteArray, docInfo.DocAscii, docInfo.Config, ".DOCX"); + bool isLibraryDocument = (docInfo.LibTitle != null && docInfo.LibTitle != ""); + Document myDoc = null; + if (isLibraryDocument) + myDoc = Document.Get(docInfo.DocID); + else + myDoc = Document.MakeDocument(null, ByteArray, docInfo.DocAscii, docInfo.Config, ".DOCX"); - // update the document information in the database - SectionInfo msi = itmInfo as SectionInfo; - Section sec = msi.Get(); - SectionConfig cfg = sec.SectionConfig; - if (!isLibraryDocument) - cfg.MySection.MyContent.MyEntry.MyDocument = myDoc; // resetting MyDocument will clear the library doc link - so don't do if a library document - else - cfg.MySection.MyContent.MyEntry.MyDocument.DocContent = ByteArray; // only update .DocContent for library documents - cfg.MySection.MyContent.MyEntry.MyDocument.FileExtension = ".DOCX"; // make sure the Word file extension is .DOCX - cfg.MySection.MyContent.MyEntry.MyDocument.DTS = fi.LastWriteTimeUtc; - cfg.MySection.MyContent.MyEntry.MyDocument.MarkDirty(); - cfg.MySection.MyContent.MyEntry.Save(); + // update the document information in the database + msi = itmInfo as SectionInfo; + sec = msi.Get(); + cfg = sec.SectionConfig; + if (!isLibraryDocument) + cfg.MySection.MyContent.MyEntry.MyDocument = myDoc; // resetting MyDocument will clear the library doc link - so don't do if a library document + else + cfg.MySection.MyContent.MyEntry.MyDocument.DocContent = ByteArray; // only update .DocContent for library documents + cfg.MySection.MyContent.MyEntry.MyDocument.FileExtension = ".DOCX"; // make sure the Word file extension is .DOCX + cfg.MySection.MyContent.MyEntry.MyDocument.DTS = fi.LastWriteTimeUtc; + cfg.MySection.MyContent.MyEntry.MyDocument.MarkDirty(); + cfg.MySection.MyContent.MyEntry.Save(); - _MyLog.InfoFormat("Converted Word Section to DOCX - Old ID {0} - New ID {1} - {2}", docInfo.DocID, myDoc.DocID, statMsg); // record in log file (aka error log) that conversion was done - // delete the temporary files - fi.Delete();// delete the temporary .DOCX file - FileInfo orgFile = new FileInfo(orgFilename); - orgFile.Delete();// delete the old temporary Word file (.DOC or .RTF) - pmsg.Close(); // close the statue message + _MyLog.InfoFormat("Converted Word Section to DOCX - Old ID {0} - New ID {1} - {2}", docInfo.DocID, myDoc.DocID, statMsg); // record in log file (aka error log) that conversion was done + // delete the temporary files + FileInfo orgFile = new FileInfo(orgFilename); + orgFile.Delete();// delete the old temporary Word file (.DOC or .RTF) + } + catch (Exception ex) + { + _MyLog.ErrorFormat("Error converting Word section to DOCX - {0}", ex.Message); + _MyLog.ErrorFormat("Error converting Word section to DOCX - ConvertWordSetionToDOXX: ItemID ={0} DOCID={1} LibTitle = {2}", itmInfo, docInfo.DocID, docInfo.LibTitle); + } + finally + { + if (pmsg != null) + pmsg.Close();// close the statue message + if (ap != null) + ap.Quit(); // close the Word app + if (doc != null) + doc.Close(); + if (fs != null) + fs.Close(); + if (fi != null && fi.Exists) + fi.Delete();// delete the temporary .DOCX file + } } ///