Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#

Word Automation

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
8 Nov 2007CPOL6 min read 145.8K   3.8K   68  
Word Automation using Early Binding and Late Binding.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Appfabs
India India
- C#, WPF, WCF
- Java, J2EE, jersey, Spring MVC, Spring Boot, Spring Security, Hibernate, Spring Data
- NoSQL with Cassandra
- C, C++, Qt, OpenGL, Python
- SQL Server, Oracle, PostgreSQL
- Glassfish, Apache, Tomcat, NodeJS and IIS
- Hadoop, Flume, Spark
- Amazon Web Services (AWS) & Jenkins
- Big Data and Analytics

Comments and Discussions