191 lines
5.2 KiB
C#
191 lines
5.2 KiB
C#
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();
|
|
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)
|
|
{
|
|
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0);
|
|
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 1);
|
|
try
|
|
{
|
|
int idx = -1;
|
|
if (_WordApp.Selection.Text.EndsWith("\t(\r") || _WordApp.Selection.Text.EndsWith("\t___\r"))
|
|
idx = _WordApp.Selection.Text.LastIndexOf("\t");
|
|
if (idx > 0)
|
|
txbWrdText.Text = _WordApp.Selection.Text.Substring(0, idx);
|
|
else
|
|
txbWrdText.Text = _WordApp.Selection.Text;
|
|
txbWrdText.Text = txbWrdText.Text.Replace('\xA0', ' '); // replace hard space with regular space
|
|
txbWrdText.Text = txbWrdText.Text.Replace('\x0B', ' '); // replace hard return with regular space
|
|
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
|
|
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
|
|
Clipboard.SetText(txbWrdText.Text);
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
|
|
private void btnNext_Click(object sender, EventArgs e)
|
|
{
|
|
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 0);
|
|
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
|
|
try
|
|
{
|
|
int idx = -1;
|
|
if (_WordApp.Selection.Text.EndsWith("\t(\r") || _WordApp.Selection.Text.EndsWith("\t___\r"))
|
|
idx = _WordApp.Selection.Text.LastIndexOf("\t");
|
|
if (idx > 0)
|
|
txbWrdText.Text = _WordApp.Selection.Text.Substring(0, idx);
|
|
else
|
|
txbWrdText.Text = _WordApp.Selection.Text;
|
|
txbWrdText.Text = txbWrdText.Text.Replace('\xA0', ' '); // replace hard space with regular space
|
|
txbWrdText.Text = txbWrdText.Text.Replace('\x0B', ' '); // replace hard return with regular space
|
|
txbWrdText.Text = txbWrdText.Text.Replace("\t", " "); // replace tabs with 5 regular spaces
|
|
|
|
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
|
|
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
|
|
Clipboard.SetText(txbWrdText.Text);
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
private void btnNextIns_Click(object sender, EventArgs e)
|
|
{
|
|
if (MyStepRTB != null)
|
|
{
|
|
if (MyStepRTB.MyItemInfo.IsSection)
|
|
{
|
|
MyStepRTB.RtbSendKeys("^{DOWN}"); // <Ctrl><down arrow> - 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}"); // <Ctrl><down arrow> - next edit window
|
|
MyStepRTB.RtbSendKeys("^a"); // select all
|
|
MyStepRTB.RtbSendKeys("^v"); // clipboard paste
|
|
Application.DoEvents();
|
|
}
|
|
this.Focus(); // set focus back to this form
|
|
}
|
|
}
|
|
}
|