tags:

views:

240

answers:

4

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");

  }

 }
+1  A: 

Is there any reason you're not using aspell.net? Office interop is, well, a little fiddly.

RobS
+1 Though I find nhunspell.sourceforge.net to be easier to use
Tinister
A: 

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#

Umaid
A: 

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

danbystrom
A: 

Hi danbystrom, 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 ?

Umaid
Please would you add these as comments to the relevant answers (or edits to your question) rather than this space which is for *adding answers*. This is not a forum. Please see the faq.
RobS
What is the type of objTempDoc, and the type of ObjWord? (And please, don't answer this by adding yet another answer.) ;)
RobS