Added code to handle conversion errors and code to create DocAscii field
This commit is contained in:
parent
8cfc46ffde
commit
3a27bf6eb6
@ -19,6 +19,7 @@ using System.Collections.Generic;
|
|||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using Volian.MSWord;
|
using Volian.MSWord;
|
||||||
using VEPROMS.CSLA.Library;
|
using VEPROMS.CSLA.Library;
|
||||||
|
|
||||||
@ -26,9 +27,9 @@ namespace DataLoader
|
|||||||
{
|
{
|
||||||
public partial class Loader
|
public partial class Loader
|
||||||
{
|
{
|
||||||
private void SaveSectionDocument(string fname, string stpseq, string stype, ref int cid)
|
private void SaveSectionDocument(string fname, string stpseq, string stype, ref int cid, string sectName)
|
||||||
{
|
{
|
||||||
int docid = SaveWordDoc(fname, stype);
|
int docid = SaveWordDoc(fname, stype, sectName);
|
||||||
switch (docid)
|
switch (docid)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
@ -45,14 +46,29 @@ namespace DataLoader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int SaveWordDoc(string fname, string title, string stype, ConfigInfo ci)
|
private int SaveWordDoc(string fname, string title, string stype, ConfigInfo ci, string sectName)
|
||||||
{
|
{
|
||||||
int docid = 0;
|
int docid = 0;
|
||||||
if (System.IO.File.Exists(fname))
|
if (System.IO.File.Exists(fname))
|
||||||
{
|
{
|
||||||
|
FileInfo myFile = new FileInfo(fname);
|
||||||
|
//string tmpName = @"C:\Temp\DataLoader\" + myFile.Name.Replace(".", "_") + ".RTF";
|
||||||
|
string tmpName = Path.GetTempFileName();
|
||||||
|
string temppath = Path.GetTempFileName();
|
||||||
if (frmMain.cbSaveDocChecked)
|
if (frmMain.cbSaveDocChecked)
|
||||||
{
|
{
|
||||||
WordDoc d = new WordDoc(fname);
|
FileInfo tmpFile = new FileInfo(tmpName);
|
||||||
|
if (tmpFile.Exists)
|
||||||
|
tmpFile.Delete();
|
||||||
|
myFile.CopyTo(tmpName);
|
||||||
|
bool keepTrying = true;
|
||||||
|
int attempt = 1;
|
||||||
|
while (keepTrying)
|
||||||
|
{
|
||||||
|
WordDoc myWordDoc = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
myWordDoc = new WordDoc(tmpName);
|
||||||
/* 16-bit's type[1] used the following codes to represent the respective lpi setting
|
/* 16-bit's type[1] used the following codes to represent the respective lpi setting
|
||||||
*
|
*
|
||||||
* char far typestr[] = "*pP46f7L";
|
* char far typestr[] = "*pP46f7L";
|
||||||
@ -76,7 +92,7 @@ namespace DataLoader
|
|||||||
else if (stype[1] == '4') lpi = 4;
|
else if (stype[1] == '4') lpi = 4;
|
||||||
else if (stype[1] == '7') lpi = 7;
|
else if (stype[1] == '7') lpi = 7;
|
||||||
// if need landscape set too: bool landscape = (stype[1] == 'L');
|
// if need landscape set too: bool landscape = (stype[1] == 'L');
|
||||||
d.SetLineSpacing(lpi); // if need landscape set too: , landscape); ;
|
myWordDoc.SetLineSpacing(lpi); // if need landscape set too: , landscape); ;
|
||||||
}
|
}
|
||||||
//string temppath = Path.GetTempFileName();
|
//string temppath = Path.GetTempFileName();
|
||||||
/* 16-bit's type[1] used the following codes to represent the respective lpi setting
|
/* 16-bit's type[1] used the following codes to represent the respective lpi setting
|
||||||
@ -102,22 +118,81 @@ namespace DataLoader
|
|||||||
else if (stype[1] == '4') lpi = 4;
|
else if (stype[1] == '4') lpi = 4;
|
||||||
else if (stype[1] == '7') lpi = 7;
|
else if (stype[1] == '7') lpi = 7;
|
||||||
// if need landscape set too: bool landscape = (stype[1] == 'L');
|
// if need landscape set too: bool landscape = (stype[1] == 'L');
|
||||||
d.SetLineSpacing(lpi); // if need landscape set too: , landscape); ;
|
myWordDoc.SetLineSpacing(lpi); // if need landscape set too: , landscape); ;
|
||||||
}
|
}
|
||||||
string temppath = Path.GetTempFileName();
|
float docLen = myWordDoc.Length;
|
||||||
string s = d.Save(temppath);
|
|
||||||
float docLen = d.Length;
|
|
||||||
d.Close();
|
|
||||||
WaitMS(wms);
|
|
||||||
if (ci == null) ci = new ConfigInfo(null);
|
if (ci == null) ci = new ConfigInfo(null);
|
||||||
ci.AddItem("Printing", "Length", string.Format("{0:0.0000}", docLen));
|
ci.AddItem("Printing", "Length", string.Format("{0:0.0000}", docLen));
|
||||||
docid = SaveDoc(temppath, title, ci);
|
string ascii = myWordDoc.MyWordDoc.Ascii;
|
||||||
File.Delete(temppath);
|
string s = myWordDoc.Save(temppath);
|
||||||
|
myWordDoc.Close();
|
||||||
|
WaitMS(wms);
|
||||||
|
docid = SaveDoc(temppath, title, ci, ascii);
|
||||||
|
DeleteFile(temppath);
|
||||||
|
DeleteFile(tmpName);
|
||||||
|
keepTrying = false;
|
||||||
|
if (attempt == 2)
|
||||||
|
{
|
||||||
|
frmMain.AddError("Success: MSWord was able to process file");
|
||||||
|
frmMain.AddError("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//frmMain.AddError(ex, "SaveWordDoc");
|
||||||
|
if (attempt == 1)
|
||||||
|
frmMain.AddError("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
|
||||||
|
else
|
||||||
|
frmMain.AddError("============================================================================");
|
||||||
|
frmMain.AddError("Attempt {0} - File Could not be opened in Word", attempt);
|
||||||
|
frmMain.AddError("{0}", fname);
|
||||||
|
frmMain.AddError("Section: '{0}'", title == string.Empty ? sectName : title);
|
||||||
|
log.Error("SaveWordDoc");
|
||||||
|
log.ErrorFormat("oldstepsequence = {0}", fname);
|
||||||
|
log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
|
||||||
|
log.ErrorFormat(ex.StackTrace);
|
||||||
|
switch (attempt)
|
||||||
|
{
|
||||||
|
case 1: // Covert using RTB
|
||||||
|
CloseMSWord(myWordDoc);
|
||||||
|
frmMain.AddError("Attempting to fix the file and try again.");
|
||||||
|
// Get the rtf from the file
|
||||||
|
string rtf = LoadFileRaw(tmpName);
|
||||||
|
// Parse for margins because the RTB will remove them
|
||||||
|
Match match = Regex.Match(rtf, @"((\\marg[^\\]+)+)", RegexOptions.Singleline);
|
||||||
|
using (RichTextBox rtb = new RichTextBox())
|
||||||
|
{
|
||||||
|
rtb.Rtf = rtf; // Allow the RichTextBox to parse the RTF
|
||||||
|
rtf = rtb.Rtf; // Get the RTF back from the RichTextBox
|
||||||
|
}
|
||||||
|
// Add the margins to the RTB output
|
||||||
|
rtf = rtf.Replace(@"\ansi", @"\ansi" + match.Value);
|
||||||
|
// Save the modified rtf to the temporary file
|
||||||
|
SaveFile(rtf, tmpName);
|
||||||
|
attempt = 2;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
frmMain.AddError("MS Word cannot process the file");
|
||||||
|
if (myWordDoc != null)
|
||||||
|
{
|
||||||
|
TryToShowMSWord(myWordDoc);
|
||||||
|
CloseMSWord(myWordDoc);
|
||||||
|
}
|
||||||
|
docid = SaveDoc(fname, title, ci, ""); // Save the original file
|
||||||
|
keepTrying = false;
|
||||||
|
DeleteFile(temppath);
|
||||||
|
DeleteFile(tmpName);
|
||||||
|
frmMain.AddError("Failure: Original contents saved to database");
|
||||||
|
frmMain.AddError("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (frmMain.cbSaveRTFChecked)
|
if (frmMain.cbSaveRTFChecked)
|
||||||
docid = SaveDoc(fname, title, ci);
|
docid = SaveDoc(fname, title, ci,""); // Need to get Ascii Text from RTF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -125,11 +200,85 @@ namespace DataLoader
|
|||||||
frmMain.AddError("Missing rtf file: {0}", fname);
|
frmMain.AddError("Missing rtf file: {0}", fname);
|
||||||
return docid;
|
return docid;
|
||||||
}
|
}
|
||||||
private int SaveWordDoc(string temppath, string stype)
|
|
||||||
|
private static void TryToShowMSWord(WordDoc myWordDoc)
|
||||||
{
|
{
|
||||||
return SaveWordDoc(temppath, String.Empty, stype, null);
|
try
|
||||||
|
{
|
||||||
|
if (MessageBox.Show("Word is having problems processing this file.\r\n\r\nDo you want to see?", "MS Word Failure", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||||||
|
{
|
||||||
|
WordDoc.MyWordApp.Visible = true;
|
||||||
|
MessageBox.Show("Click on OK when you are done with MS Word", "Wait until done with MS Word", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
}
|
}
|
||||||
private int SaveTheDoc(string temppath, string title, ConfigInfo ci)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CloseMSWord(WordDoc myWordDoc)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (myWordDoc != null)
|
||||||
|
{
|
||||||
|
WordDoc.CloseApp();
|
||||||
|
Application.DoEvents();
|
||||||
|
while (WordDoc.WordProcesses.Length > 0)
|
||||||
|
Application.DoEvents();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static void DeleteFile(string fName)
|
||||||
|
{
|
||||||
|
if (File.Exists(fName))
|
||||||
|
File.Delete(fName);
|
||||||
|
}
|
||||||
|
private string LoadFileRaw(string fName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileInfo file = new FileInfo(fName);
|
||||||
|
byte[] myBytes = new byte[file.Length];
|
||||||
|
using (FileStream fs = file.OpenRead())
|
||||||
|
{
|
||||||
|
fs.Read(myBytes, 0, (int)file.Length);
|
||||||
|
fs.Close();
|
||||||
|
}
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
foreach (byte byt in myBytes)
|
||||||
|
{
|
||||||
|
int iByte = (int)byt;
|
||||||
|
sb.Append((char)byt);
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message);
|
||||||
|
Application.DoEvents();
|
||||||
|
return LoadFileRaw(fName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void SaveFile(string str, string fName)
|
||||||
|
{
|
||||||
|
FileInfo file = new FileInfo(fName);
|
||||||
|
using (StreamWriter sw = file.CreateText())
|
||||||
|
{
|
||||||
|
sw.Write(str);
|
||||||
|
sw.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private int SaveWordDoc(string temppath, string stype, string sectName)
|
||||||
|
{
|
||||||
|
return SaveWordDoc(temppath, String.Empty, stype, null, sectName);
|
||||||
|
}
|
||||||
|
private int SaveTheDoc(string temppath, string title, ConfigInfo ci, string ascii)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -144,7 +293,8 @@ namespace DataLoader
|
|||||||
string docfile = temppath.Substring(0, temppath.LastIndexOf(".")) + @".doc";
|
string docfile = temppath.Substring(0, temppath.LastIndexOf(".")) + @".doc";
|
||||||
if (File.Exists(docfile)) File.Delete(docfile);
|
if (File.Exists(docfile)) File.Delete(docfile);
|
||||||
FileInfo doctmpFile = tmpFile.CopyTo(docfile);
|
FileInfo doctmpFile = tmpFile.CopyTo(docfile);
|
||||||
doc.UpdateDocAscii(docfile);
|
doc.DocAscii = ascii;
|
||||||
|
//doc.UpdateDocAscii(docfile);
|
||||||
doc.Save();
|
doc.Save();
|
||||||
File.Delete(docfile);
|
File.Delete(docfile);
|
||||||
DocumentInfo di = DocumentInfo.Get(doc.DocID);
|
DocumentInfo di = DocumentInfo.Get(doc.DocID);
|
||||||
@ -167,14 +317,14 @@ namespace DataLoader
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
private int SaveDoc(string temppath, string title, ConfigInfo ci)
|
private int SaveDoc(string temppath, string title, ConfigInfo ci, string ascii)
|
||||||
{
|
{
|
||||||
int done = 0;
|
int done = 0;
|
||||||
int ntry = 0;
|
int ntry = 0;
|
||||||
while (done == 0 && ntry < 4)
|
while (done == 0 && ntry < 4)
|
||||||
{
|
{
|
||||||
ntry++;
|
ntry++;
|
||||||
done = SaveTheDoc(temppath, title, ci);
|
done = SaveTheDoc(temppath, title, ci, ascii);
|
||||||
}
|
}
|
||||||
return done;
|
return done;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user