|
using System;
using System.IO;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using Automation;
namespace AutomationTest
{
public partial class Automation : Form
{
private object _wordApplication = null;
private object _wordDocument = null;
public Automation()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
WordAutomation automation = new WordAutomation();
if (_wordApplication == null)
{
_wordApplication = automation.CreateWordApplication();
}
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All word documents(*.doc)|*.doc";
if (DialogResult.OK == dlg.ShowDialog(this))
{
if (File.Exists(dlg.FileName))
{
if (_wordDocument != null)
{
automation.CloseWordDoc(_wordDocument, true);
}
_wordDocument = automation.CreateWordDoc(
dlg.FileName, _wordApplication, false);
txtFileName.Text = dlg.FileName;
}
}
}
private void btnGetCount_Click(object sender, EventArgs e)
{
lblCount.Text = string.Empty;
if (_wordApplication != null && _wordDocument != null)
{
if (txtFind.Text.Trim().Length == 0)
{
MessageBox.Show("Enter a text to find.", AppName,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
int count = new WordAutomation().GetWordCount(
_wordDocument, txtFind.Text.Trim());
lblCount.Text = count.ToString();
}
else
{
MessageBox.Show("Open a valid document to continue.", AppName,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
public string AppName
{
get
{
return "Word Automation";
}
}
private void Automation_FormClosing(object sender, FormClosingEventArgs e)
{
if (_wordDocument != null)
{
new WordAutomation().CloseWordDoc(_wordDocument, true);
}
if (_wordApplication != null)
{
new WordAutomation().CloseWordApp(_wordApplication);
}
}
private void btnReplaceAll_Click(object sender, EventArgs e)
{
if (_wordApplication != null && _wordDocument != null)
{
if (txtFind.Text.Trim().Length == 0)
{
MessageBox.Show("Enter a text to find.", AppName,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (txtReplace.Text.Trim().Length == 0)
{
MessageBox.Show("Enter a text to replace.", AppName,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
new WordAutomation().FindReplace(_wordDocument,
_wordApplication, txtFind.Text.Trim(), txtReplace.Text.Trim());
MessageBox.Show("Replaced successfully.", AppName,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Open a valid document to continue.", AppName,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.