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(); } 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); try { int idx = _WordApp.Selection.Text.IndexOfAny("\t".ToCharArray()); 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(); //_WordApp.Selection.Copy(); 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 = _WordApp.Selection.Text.IndexOfAny("\t".ToCharArray()); 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(); //_WordApp.Selection.Copy(); Clipboard.SetText(txbWrdText.Text); } catch { } //_WordApp.Selection.Copy(); //string tmp = Clipboard.GetText(TextDataFormat.Html); //EditItem ei = MyDisplayTabItem.MyStepTabPanel.SelectedEditItem; //while (ei.Enabled == false) // ei = ei.MyParentEditItem ?? ei.MyPreviousEditItem; //ei.MyStepRTB.Focus(); } 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 } } } }