using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LBWordLibrary;
namespace Volian.Controls.Library
{
public partial class frmImportWordContents : Form
{
LBApplicationClass _WordApp;
bool _initializing = false;
private StepRTB _MyStepRTB = null;
public StepRTB MyStepRTB
{
get { return _MyStepRTB; }
set { _MyStepRTB = value; }
}
public frmImportWordContents()
{
_initializing = true;
InitializeComponent();
// C2019-021 Allow the Number field to be edited.
txbWordFile.Text = Properties.Settings.Default.ImportWordFilePath;
disableButtons();
_initializing = false;
btnOpen.Enabled = (txbWordFile.Text.Length > 0);
}
private void disableButtons()
{
btnOpen.Enabled = false;
btnNext.Enabled = false;
btnPage.Enabled = false;
btnPrev.Enabled = false;
}
private void ofd_FileOk(object sender, CancelEventArgs e)
{
_initializing = true;
txbWordFile.Text = ofd.FileName;
_initializing = false;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// if we have a word doc open, then close it and reset WordApp
if (_WordApp != null)
{
try
{
_WordApp.Quit();
}
catch
{
// will error if user already closed Word
}
_WordApp = null;
disableButtons();
}
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
btnOpen_Click(sender, e);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (!File.Exists(txbWordFile.Text))
{
MessageBox.Show(string.Format("{0}", txbWordFile.Text), "File Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
disableButtons();
return;
}
_WordApp = new LBApplicationClass();
_WordApp.Documents.Open(txbWordFile.Text);
_WordApp.Visible = true;
btnNext.Enabled = true;
btnPage.Enabled = true;
btnPrev.Enabled = true;
Properties.Settings.Default.ImportWordFilePath = txbWordFile.Text;
Properties.Settings.Default.Save();
}
private void btnPage_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveDown(LBWdUnits.wdScreen, 1, 0);
}
private void btnPrev_Click(object sender, EventArgs e)
{
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0); // Move to previous paragraph
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 1); // Select paragraph
//_WordApp.Selection.MoveEnd(LBWdUnits.wdCharacter, -1); // Move 1 Character Back
try
{
// C2019-021 Use generic function CopyWordText
CopyWordText();
if (txbWrdText.Text.TrimEnd("\r\n\f".ToCharArray()) == "")
btnPrev_Click(sender, e);
else
Clipboard.SetText(txbWrdText.Text);
}
catch
{ }
}
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
///
/// MoveToNextCell
/// Find the next Cell including merged cells
/// Loop through columns then rows
///
///
/// Yes - if Processed as a cell
/// No - Not Processed
///
private bool MoveToNextCell()
{
// Get the row and column for the current selection within the table.
int rowIndex = _WordApp.Selection.Cells[1].RowIndex;
int columnIndex = _WordApp.Selection.Cells[1].ColumnIndex;
// Only do this for tables
if (_WordApp.Selection.Tables != null && _WordApp.Selection.Tables.Count > 0)
{
LBTable tbl = _WordApp.Selection.Tables[1];
if (tbl.Range.Cells != null && tbl.Range.Cells.Count > 0)
{
// Make sure the entire cell is selected
_WordApp.Selection.SelectCell();
// Loop through the cells of the table
for (int i = 1; i < tbl.Range.Cells.Count; i++)
{
LBCell myCell = tbl.Range.Cells[i];
// if this cell has matching indices, then move to the next cell
if (myCell.RowIndex == rowIndex && myCell.ColumnIndex == columnIndex)
{
// Move the Start to the start of the next cell
_WordApp.Selection.Start = tbl.Range.Cells[i + 1].Range.Start;
// Select the entire cell
_WordApp.Selection.SelectCell();
return true; // Cell Found and moved
}
}
}
}
return false; // Cell not found
}
private void btnNext_Click(object sender, EventArgs e)
{
// C2019-021 Either move by cells or paragraphs
if (_WordApp.Selection.Cells != null && _WordApp.Selection.Cells.Count > 0)
{
if (!MoveToNextCell())
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 2, 0);// Move beyond the table
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);// Select a Paragraph
}
}
else // not a table
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 0); // Move to the next paragraph
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1); // Select a Paragraph
}
try
{
// C2019-021 Use Generic CopyWordText
CopyWordText();
if (_WordApp.Selection.Cells != null && _WordApp.Selection.Cells.Count > 0)
{
Clipboard.SetText(txbWrdText.Text);
}
else
{
// C2019-021 If the word text only contains whitespace skip to next
if (txbWrdText.Text.TrimEnd("\f\r\n".ToCharArray()) == "")
btnNext_Click(sender, e);
else
Clipboard.SetText(txbWrdText.Text);
}
}
catch
{ }
}
// C2019-021 Generic CopyWordText (Copy Text, Style and Number to fields on form)
private void CopyWordText()
{
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
tbxNumber.Text = _WordApp.Selection.Range.ListFormat.ListString;
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
try
{
//Console.WriteLine("Cell Count = {0}", _WordApp.Selection.Cells.Count);
lblTable.Text = "Table";
lblTable.FontBold = true;
}
catch
{
lblTable.Text = "Not Table";
lblTable.FontBold = false;
}
txbWrdText.Text = ShowText(_WordApp.Selection.Text.TrimEnd("\r\a".ToCharArray()));
_WordApp.Activate();
}
// C2019-021 RHM 5/15/2019 Added newe methods and properties for Import Word Content
private string ShowText(string txt)
{
// This was for debugging
//StringBuilder sb = new StringBuilder();
//foreach (char c in txt)
// if (((int)c) >= ' ' && ((int)c) < '\x7F')
// sb.Append(c);
// else
// sb.Append(string.Format("\\u{0:X4} ", ((int)c)));
//return sb.ToString();
return txt;
}
private void txbWordFile_TextChanged(object sender, EventArgs e)
{
btnOpen.Enabled = !_initializing;
}
private void frmImportWordContents_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (_WordApp != null)
_WordApp.Quit();
}
catch
{
// incase user manually closed word
}
}
//C2019-021 Remove old method.
//private void btnNextIns_Click(object sender, EventArgs e)
//{
// if (MyStepRTB != null)
// {
// if (MyStepRTB.MyItemInfo.IsSection)
// {
// MyStepRTB.RtbSendKeys("^{DOWN}"); // - next edit window
// MyStepRTB.RtbSendKeys("^+b"); // insert previous
// }
// else
// MyStepRTB.RtbSendKeys("^+i"); // insert next (same type and level)
// MyStepRTB.RtbSendKeys("^v"); // clipboard paste
// Application.DoEvents();
// }
// this.Focus(); // set focus back to this form
//}
//private void btnNextRpl_Click(object sender, EventArgs e)
//{
// if (MyStepRTB != null)
// {
// MyStepRTB.RtbSendKeys("^{DOWN}"); // - next edit window
// MyStepRTB.RtbSendKeys("^a"); // select all
// MyStepRTB.RtbSendKeys("^v"); // clipboard paste
// Application.DoEvents();
// }
// this.Focus(); // set focus back to this form
//}
// C2019-021 New function to replace the PROMS text with the word text and move to the next paragraph in word.
private void btnReplaceNext_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
if (MyStepRTB.Parent is VlnFlexGrid)//Replace text in a Grid (Table cell)
{
VlnFlexGrid vg = MyStepRTB.Parent as VlnFlexGrid;
vg.StartEditing();
MyStepRTB.Text = txbWrdText.Text;
vg[vg.Row, vg.Col] = MyStepRTB.Rtf;
vg.FinishEditing(false);
}
else if (MyStepRTB.MyItemInfo.IsSection && tbxNumber.Text != "")
{
using (VEPROMS.CSLA.Library.Item ii = MyStepRTB.MyItemInfo.Get())
{
ii.MyContent.Text = txbWrdText.Text.Trim("\r\n\f".ToCharArray());
ii.MyContent.Number = tbxNumber.Text;
ii.MyContent.Save();
}
EditItem ei = MyStepRTB.Parent as EditItem;
ei.RefreshTab();
}
else
{
MyStepRTB.Text = txbWrdText.Text.Trim("\r\n".ToCharArray());
MyStepRTB.Select(MyStepRTB.TextLength, 0);
}
btnNext_Click(sender, e);
}
}
// C2019-021 New function to insert the Word text into PROMS and move to the next paragraph in Word
private void btnInsertNext_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = txbWrdText.Text.Trim("\r\n\f".ToCharArray());
MyStepRTB.Select(MyStepRTB.TextLength, 0);
btnNext_Click(sender, e);
}
}
// C2019-021 New Function to create a after node in PROMS
private void btnAfter_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
if (MyStepRTB.Parent is VlnFlexGrid)
{
VlnFlexGrid vg = MyStepRTB.Parent as VlnFlexGrid;
//vg.FinishEditing(false);
vg.SelectNextCell();
}
else
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
}
// C2019-021 Add a New Section in PROMS (Default Type = 10000)
private void btnSect_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei.MyItemInfo.IsProcedure)
{
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Section, 10000);// TODO: Need Type for Procedure Steps
return;
}
while (ei != null && !ei.MyItemInfo.IsSection)
if (ei.MyParentEditItem != null)
ei = ei.MyParentEditItem;
else
ei = ei.MyPreviousEditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
// C201-021 New Function Add a new High level step
private void btnHigh_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei.MyItemInfo.IsSection)
{
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Step, 20002);
return;
}
while (ei != null && !ei.MyItemInfo.IsHigh)
if (ei.MyParentEditItem != null)
ei = ei.MyParentEditItem;
else
ei = ei.MyPreviousEditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
// C2019-021 New Function Add a new sequential substep
private void btnSEQ_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Step, 20001);
}
}
// C2019-021 New Function Add a new parent step after
private void btnParentAfter_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
while (ei.MyPreviousEditItem != null)
ei = ei.MyPreviousEditItem;
if (ei != null && ei.MyParentEditItem != null)
{
ei.MyParentEditItem.AddSiblingAfter();
}
}
}
// C2019-021 New Function Split the text in the current proms window into two
// The text before the selection is kept in the current PROMS window
// The selected text is deleted
// the text after the selection is cut to the clipboard
private void btnSplit_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = "";
MyStepRTB.Select(MyStepRTB.SelectionStart, MyStepRTB.TextLength - MyStepRTB.SelectionStart);
Clipboard.SetText(MyStepRTB.SelectedText);
MyStepRTB.SelectedText = "";
}
}
// C2019-021 New Function - Performs a paste in the current window
private void btnPaste_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = Clipboard.GetText();
}
}
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
private void btnCurrent_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0); // Select paragraph
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1); // Select paragraph
_WordApp.Selection.MoveEnd(LBWdUnits.wdCharacter, -1); // Exclude the last character
try
{
// C2019-021 Use Generic CopyWordText
CopyWordText();
// C2019-021If the word text only contains whitespace skip to next
//if (txbWrdText.Text.TrimEnd("\f\r\n\a".ToCharArray()) == "")
// btnNext_Click(sender, e);
//else
Clipboard.SetText(txbWrdText.Text);
}
catch
{ }
}
private void btnTest_Click(object sender, EventArgs e)
{
if (!MoveToNextCell())
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 2, 0);
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
}
_WordApp.Activate();
//int cols = _WordApp.Selection.Tables[1].Columns.Count;
}
}
}