From bf9985110a66fcba8896f93022971b4ed56a8a83 Mon Sep 17 00:00:00 2001 From: Rich Date: Wed, 25 Jul 2012 21:39:01 +0000 Subject: [PATCH] Read and Save MSWord documents with RichTextBox before they are opened in word to reduce manual fixes to accessory pages Added logic to replace sbkpage, sect, and sectd RTF tokens. Use one RichTextBox to process text since RichTextBoxes are not released if the RTF value is set --- PROMS/DataLoader/Documents.cs | 40 +++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/PROMS/DataLoader/Documents.cs b/PROMS/DataLoader/Documents.cs index f853adc6..092ce8aa 100644 --- a/PROMS/DataLoader/Documents.cs +++ b/PROMS/DataLoader/Documents.cs @@ -71,6 +71,7 @@ namespace DataLoader WordDoc myWordDoc = null; try { + LoadAndSaveRichTextBox(tmpName); myWordDoc = new WordDoc(tmpName); /* 16-bit's type[1] used the following codes to represent the respective lpi setting * @@ -148,18 +149,7 @@ namespace DataLoader 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); + LoadAndSaveRichTextBox(tmpName); attempt = 2; break; default: @@ -191,7 +181,31 @@ namespace DataLoader frmMain.AddError("Missing rtf file: {0}", fname); return docid; } - + private RichTextBox _MyRichTextBox = null; + public RichTextBox MyRichTextBox + { + get + { + if (_MyRichTextBox == null) + _MyRichTextBox = new RichTextBox(); + return _MyRichTextBox; + } + } + private void LoadAndSaveRichTextBox(string tmpName) + { + string rtf = LoadFileRaw(tmpName); + // Parse for margins because the RTB will remove them + Match match = Regex.Match(rtf, @"((\\marg[^\\]+)+)", RegexOptions.Singleline); + rtf = rtf.Replace("\\sectd", ""); + rtf = rtf.Replace("\\sbkpage", ""); + rtf = rtf.Replace("\\sect", "\\page"); + MyRichTextBox.Rtf = rtf; // Allow the RichTextBox to parse the RTF + rtf = MyRichTextBox.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); + } private static void TryToShowMSWord(WordDoc myWordDoc) { try