SourceCode/PROMS/Volian.Controls.Library/frmImportWordContents.cs
John 01f7850964 Added a button for importing text from a word document
Allows you to select/open a word document and will put word text on clipboard
To remember the last word document opened
2016-03-11 15:33:25 +00:00

130 lines
3.3 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;
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;
//btnOpen.Enabled = true;
}
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(false);
}
catch
{
// will error if user already closed Word
}
_WordApp = null;
disableButtons();
}
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
btnOpen_Click(sender, e);
// btnOpen.PerformClick(); // does not work if button is disabled
}
}
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);
txbWrdText.Text = _WordApp.Selection.Text;
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
//_WordApp.Selection.Copy();
Clipboard.SetText(txbWrdText.Text);
}
private void btnNext_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 0);
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
txbWrdText.Text = _WordApp.Selection.Text;
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
//_WordApp.Selection.Copy();
Clipboard.SetText(txbWrdText.Text);
}
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(false);
}
catch
{
// incase user manually closed word
}
}
}
}