C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |



My Profile

Gifts

Active Members
TodayLast 7 Days more...









Automating Word to accommodate Spell Check Functionality in your .Net windows application


Posted Date: 02 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: shakti singh tanwar       Member Level: Diamond
Rating:     Points: 10



Introduction



Automation is something that really fascinated me from the time I have started coding and I love it simply because you can use the entire features of an existing mammoth application in your application by juts importing the appropriate Dll and calling the exposed API’s. In this article I am going to show how to call Microsoft word API’s to incorporate spell check functionality in your applications.
For this sample to run you need to first add a reference to word Dll.
Microsoft office 2007 Users
Add a reference to Microsoft word 12.0 object library

When you add a reference to the above dll you will see three references added to your project viz Microsoft.Office.Core, Microsoft.Office.Interop.Word and VBIDE. Import the namespace Microsoft.Office.Interop.Word in your project so that you don’t need to completely qualify the class names with the namespace but by doing this you will get an ambiguity when you access Application class because it is present in both System namespace as well as Microsoft.Office.Interop.Word namespace so completely qualify it whenever needed.

This particular sample is made for office 2007 users to make it run with office 2000/XP just make following changes:-
1. Add a reference to Microsoft word 11.0 object library
2. Import Word namespace
3. Instead of Microsoft.Office.Interop.Word.Application use Word.Application

Now create a new windows application project and delete the program.cs and Form1.designer.cs files from the project. Copy the code below and replace the code of Form1.cs with it. Compile your application and run.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Office.Interop.Word;
using System.Reflection;

namespace SpellCheckDemo
{
///
/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnSpellCheck;
private System.Windows.Forms.Label label1;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnSpellCheck = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(40, 40);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(344, 136);
this.textBox1.TabIndex = 0;
//
// btnSpellCheck
//
this.btnSpellCheck.Location = new System.Drawing.Point(392, 40);
this.btnSpellCheck.Name = "btnSpellCheck";
this.btnSpellCheck.Size = new System.Drawing.Size(96, 23);
this.btnSpellCheck.TabIndex = 1;
this.btnSpellCheck.Text = "Spell Check";
this.btnSpellCheck.Click += new System.EventHandler(this.btnSpellCheck_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(336, 16);
this.label1.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 205);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnSpellCheck);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "SpellCheckDemo";
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form1());
}

private void btnSpellCheck_Click(object sender, System.EventArgs e)
{
SpellCheck(textBox1 , label1 );
}

public void SpellCheck(TextBox tBox, Label lLbl)
{
int iErrorCount = 0;
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
if (tBox.Text.Length > 0)
{
app.Visible=false;
// Setting these variables is comparable to passing null to the function.
// This is necessary because the C# null cannot be passed by reference.
object template=Missing.Value;
object newTemplate=Missing.Value;
object documentType=Missing.Value;
object visible=true;
object optional = Missing.Value;

_Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
doc.Words.First.InsertBefore (tBox.Text );
ProofreadingErrors we = doc.SpellingErrors;
iErrorCount = we.Count;

doc.CheckSpelling( ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref optional, ref optional);

if (iErrorCount == 0)
lLbl.Text = "Spelling is correct. No errors corrected ";
else if (iErrorCount == 1)
lLbl.Text = "Spelling is correct now. 1 error corrected ";
else
lLbl.Text = "Spelling is correct now. " + iErrorCount + " errors corrected ";
object first=0;
object last=doc.Characters.Count -1;

tBox.Text = doc.Range(ref first, ref last).Text;
}
else
lLbl.Text = "Textbox is empty";

object saveChanges = false;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
}
}
}





Responses

Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Author: Nanak Deep    18 Jun 2008Member Level: Bronze   Points : 0
great.........

very happy


Author: Gaurav Agrawal    19 Jun 2008Member Level: Silver   Points : 2
Hi sakti,
Very good and detailed article.
It is very helpful for me.
Again thanks to share your knowledge with us.
Keep Posting and help your fellow developers.

Regards
Gaurav Agrawal


Author: muthuvasagan    20 Jun 2008Member Level: Silver   Points : 0
Its very Helpful....


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Boxing and UnBoxing
Previous Resource: Garbage Collcetion
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing

Contact Us    Privacy Policy    Terms Of Use