B2023-093 added a Try Catch to the Convert method per suggestion from review
This commit is contained in:
parent
ef3efe2380
commit
6d5d87f04d
@ -169,67 +169,96 @@ namespace VEPROMS.CSLA.Library
|
|||||||
// check the Word file extension that is saved in the tblDocuments SQL database table
|
// check the Word file extension that is saved in the tblDocuments SQL database table
|
||||||
DocumentInfo docInfo = itmInfo.MyContent.MyEntry.MyDocument;
|
DocumentInfo docInfo = itmInfo.MyContent.MyEntry.MyDocument;
|
||||||
if (docInfo.FileExtension.ToUpper() == ".DOCX") return; // already a DOCX - no need to convert
|
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
|
try
|
||||||
// use the section number (DisplayNumber) unless the length is zero, then use the section title (DisplayText)
|
{
|
||||||
string statMsg = itmInfo.DisplayNumber;
|
// show user a status window of the Word section being converted to DOCX
|
||||||
if (statMsg.Length == 0)
|
// use the section number (DisplayNumber) unless the length is zero, then use the section title (DisplayText)
|
||||||
statMsg = itmInfo.DisplayText;
|
string statMsg = itmInfo.DisplayNumber;
|
||||||
FrmPopupStatusMessage pmsg = new FrmPopupStatusMessage("Converting This Section to Word DOCX", statMsg);
|
if (statMsg.Length == 0)
|
||||||
pmsg.Show();
|
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)
|
// 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);
|
myfile = new DSOFile(docInfo);
|
||||||
|
|
||||||
// Open MSWord App
|
// Open MSWord App
|
||||||
LBWordLibrary.LBApplicationClass ap = new LBWordLibrary.LBApplicationClass();
|
ap = new LBWordLibrary.LBApplicationClass();
|
||||||
LBWordLibrary.LBDocumentClass doc = ap.Documents.Open(myfile.FullName);
|
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
|
// 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
|
// In either case, we want to convert it to a Word DOCX file
|
||||||
// So now create a file name with the .DOCX extension
|
// So now create a file name with the .DOCX extension
|
||||||
string orgFilename = myfile.FullName.ToUpper();
|
orgFilename = myfile.FullName.ToUpper();
|
||||||
string filename = orgFilename.Replace(".RTF", ".DOCX").Replace(".DOC", ".DOCX"); // we want to convert either .RTF or .DOC Word sections
|
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
|
// 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.SaveAs(filename, LBWordLibrary.LBWdSaveFormat.wdFormatXMLDocument); // Convert to Word DOCX
|
||||||
doc.Close();
|
doc.Close();
|
||||||
ap.Quit(); // close the Word app
|
doc = null;
|
||||||
|
|
||||||
// Now read in the new .DOCX file and save the contents to the SQL database
|
// Now read in the new .DOCX file and save the contents to the SQL database
|
||||||
FileInfo fi = new FileInfo(filename);
|
fi = new FileInfo(filename);
|
||||||
FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);// B2016-053
|
fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);// B2016-053
|
||||||
long len = fs.Length;
|
long len = fs.Length;
|
||||||
byte[] ByteArray = new byte[len];
|
byte[] ByteArray = new byte[len];
|
||||||
int nBytesRead = fs.Read(ByteArray, 0, (int)len);
|
int nBytesRead = fs.Read(ByteArray, 0, (int)len);
|
||||||
fs.Close();
|
|
||||||
|
|
||||||
bool isLibraryDocument = (docInfo.LibTitle != null && docInfo.LibTitle != "");
|
bool isLibraryDocument = (docInfo.LibTitle != null && docInfo.LibTitle != "");
|
||||||
Document myDoc = null;
|
Document myDoc = null;
|
||||||
if (isLibraryDocument)
|
if (isLibraryDocument)
|
||||||
myDoc = Document.Get(docInfo.DocID);
|
myDoc = Document.Get(docInfo.DocID);
|
||||||
else
|
else
|
||||||
myDoc = Document.MakeDocument(null, ByteArray, docInfo.DocAscii, docInfo.Config, ".DOCX");
|
myDoc = Document.MakeDocument(null, ByteArray, docInfo.DocAscii, docInfo.Config, ".DOCX");
|
||||||
|
|
||||||
// update the document information in the database
|
// update the document information in the database
|
||||||
SectionInfo msi = itmInfo as SectionInfo;
|
msi = itmInfo as SectionInfo;
|
||||||
Section sec = msi.Get();
|
sec = msi.Get();
|
||||||
SectionConfig cfg = sec.SectionConfig;
|
cfg = sec.SectionConfig;
|
||||||
if (!isLibraryDocument)
|
if (!isLibraryDocument)
|
||||||
cfg.MySection.MyContent.MyEntry.MyDocument = myDoc; // resetting MyDocument will clear the library doc link - so don't do if a library document
|
cfg.MySection.MyContent.MyEntry.MyDocument = myDoc; // resetting MyDocument will clear the library doc link - so don't do if a library document
|
||||||
else
|
else
|
||||||
cfg.MySection.MyContent.MyEntry.MyDocument.DocContent = ByteArray; // only update .DocContent for library documents
|
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.FileExtension = ".DOCX"; // make sure the Word file extension is .DOCX
|
||||||
cfg.MySection.MyContent.MyEntry.MyDocument.DTS = fi.LastWriteTimeUtc;
|
cfg.MySection.MyContent.MyEntry.MyDocument.DTS = fi.LastWriteTimeUtc;
|
||||||
cfg.MySection.MyContent.MyEntry.MyDocument.MarkDirty();
|
cfg.MySection.MyContent.MyEntry.MyDocument.MarkDirty();
|
||||||
cfg.MySection.MyContent.MyEntry.Save();
|
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
|
_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
|
// delete the temporary files
|
||||||
fi.Delete();// delete the temporary .DOCX file
|
FileInfo orgFile = new FileInfo(orgFilename);
|
||||||
FileInfo orgFile = new FileInfo(orgFilename);
|
orgFile.Delete();// delete the old temporary Word file (.DOC or .RTF)
|
||||||
orgFile.Delete();// delete the old temporary Word file (.DOC or .RTF)
|
}
|
||||||
pmsg.Close(); // close the statue message
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user