Search code examples
c#office-2007

I am integrating Spell Checker for my application


Hi I am creating Spell Checker for my c# application. I am using using Microsoft.Office.Interop.Word; dlls but having error.

I have tested my code in VB.Net and it is working fi9 but now I have to c# and having errors in my code

private void SpellOrGrammarCheck(bool blnSpellOnly)
    {

        try
        {
            object objWord;
            object objTempDoc;
            IDataObject iData;

            if (TextBox1.Text == "")
            {
                return;
            }

            objWord = new Microsoft.Office.Interop.Word.Application();
            objTempDoc = objWord.Documents.Add();
            objWord.Visible = false;

            objWord.WindowState = 0;
            objWord.Top = - 3000;

            Clipboard.SetDataObject(TextBox1.Text);

            objTempDoc.Content.Paste();
            objTempDoc.Activate();
            if (blnSpellOnly)
            {
                objTempDoc.CheckSpelling();
            }
            else
            {
                objTempDoc.CheckGrammar();
            }
            objTempDoc.Content.Copy();
            iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
            {
                TextBox1.Text = System.Convert.ToString(iData.GetData(DataFormats.Text, System.Convert.ToBoolean(null)));
            }
            objTempDoc.Saved = true;
            objTempDoc.Close();

            objWord.Quit();

            MessageBox.Show("The spelling check is complete.", "Spell Checker", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        catch (System.Runtime.InteropServices.COMException)
        {
            MessageBox.Show("Microsoft Word must be installed for Spell/Grammar Check " + "to run.", "Spell Checker");

        }
        catch (Exception)
        {
            MessageBox.Show("An error has occurred.", "Spell Checker");

        }

    }

Object doestnot contain definition of Document. This is error which I am getting on building. I have include Reference of Microsoft.Office.Interop.Word successfully in c#


I tried your suggestion although it is good but didn't work for me. Now it is throwing new exception that

objTempDoc = objWord.Documents.Add();

Exception : No overload for method Add takes '0' arguments.

Any suggestion ?


Solution

  • C# does not support late binding. You must declare objWord as Microsoft.Office.Interop.Word.Application instead of object. Dito for objTempDoc (whatever type Documents.Add returns).