Partager via


How to: Use COM Interop to Check Spelling Using Word (C# Programming Guide) 

The following code example illustrates how to use COM Interoperability to use Word's spell-checking capabilities in your Visual C# application. For more information, see ProofreadingErrors and Microsoft Word Objects.

Example

This example illustrates how to use Word's spelling checker from a C# application. It creates a new Word.application object using COM Interoperability. It then uses the ProofreadingErrors collection on a Range object and finds the misspelled words in that range.

using System.Reflection;
using Word = Microsoft.Office.Interop.Word;

namespace WordSpell
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;

        public Form1()  //constructor
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Word.Application app = new Word.Application();

            int errors = 0;
            if (textBox1.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;

                Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
                doc1.Words.First.InsertBefore(textBox1.Text);
                Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
                errors = spellErrorsColl.Count;

                object optional = Missing.Value;

                doc1.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);

                label1.Text = errors + " errors corrected ";
                object first = 0;
                object last = doc1.Characters.Count - 1;
                textBox1.Text = doc1.Range(ref first, ref last).Text;
            }

            object saveChanges = false;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;

            app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
        }
    }
}

Compiling the Code

This example requires Word to be installed on your system and depending on the version of Office you have installed, the Word assembly may be called Microsoft office 10 Object Library or Word 11 Object Library.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To compile the code

  1. Create a new C# Windows Application project in Visual Studio, and call it WordSpell.

  2. Copy the code above, and paste it over the contents of the Form1.cs file.

  3. Copy the following code, and paste it in the Form1.Designer.cs file, in the InitializeComponent() method, after any existing code.

    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = 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;
    this.textBox1.Text = "";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(392, 40);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(96, 23);
    this.button1.TabIndex = 1;
    this.button1.Text = "Check Spelling";
    this.button1.Click += new System.EventHandler(this.button1_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.AutoScaleDimensions = new System.Drawing.SizeF(5, 13);
    this.ClientSize = new System.Drawing.Size(496, 205);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "SpellCheckDemo";
    this.ResumeLayout(false);
    
  4. Add the Word assembly as a reference to the project. Right-click on the project, click Add Reference, click the COM tab of the Add Reference dialog box. Double-click Microsoft Office 11 Object Library, and press OK.

    Note that the dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

Security

To use COM Interoperability, you must have administrator or power-user security permissions. For more information, see .NET Framework Security.

See Also

Tasks

How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide)

Concepts

C# Programming Guide
Interoperability Overview (C# Programming Guide)

Other Resources

COM Interoperability Samples