C# WinForm Spell Checking

G

Guest

Hello,
I am passing a string to a spell checking function which performs spell
check using word objects. It works fine but even though i have said
oWord.Visible = false;
the word application opens for a moment and closes ,which makes my winform
application look bad. Is there any way to supress that.
The C# function i wrote is below.

private String SpellCheck(String str)
{
try
{
object oMissing = System.Reflection.Missing.Value;

String retVal = "";

Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = false;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

oDoc.Content.Text = str;

oDoc.CheckGrammar();

oDoc.Content.Copy();


retVal =
Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
oDoc.Saved = true;
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);

oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

return retVal;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " : " + ex.Source + " : " +
ex.StackTrace);
return "Fatal Error";
}

}
 
S

Scott.Numbers

You probably found a solution by now, so I'll post this for future
readers...

I found that you can set the App Window size and location without any
adverse affects.

// After creating oWord below, add ...
oWord.Visisble = false;
oWord.Top = -1;
oWord.Left = -1;
oWord.Width = 1;
oWord.Height = 1;

This places the App Window off the screen and elimantes the flash when
the spellcheck is completed.

Hope this helps somebody.

Scott #s
 
S

Scott.Numbers

I Spoke too soon about not having adverse affects. I later found that
Word would open as small as possible in the upper left corner after
running doing this. Below is a function that will avoid the flicker
and reset the window position when completed. You might note that I had
problems with Word adding a Carriage Return to the end of the text that
I later stripped off if it was unwanted.

Hope this helps somebody,
Scott #s


using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;

....

public String CheckSpelling(String Txt) {

int iTop;
int iLeft;
int iHeight;
int iWidth;
object oMissing = System.Reflection.Missing.Value;
object oFalse = false;
object oTrue = true;
String CheckedTxt = ""; Default for Error Conditions

try {
Word.ApplicationClass wrdApp = new Word.ApplicationClass();
wrdApp.Visible = false;

iTop = wrdApp.Top;
iLeft = wrdApp.Left;
iHeight = wrdApp.Height;
iWidth = wrdApp.Width;

wrdApp.Move(-100,-100);
wrdApp.Resize(1,1);

Word.Document wrdDoc = wrdApp.Documents.Add(ref oMissing, ref
oMissing, ref oMissing, ref oTrue);

wrdDoc.Content.Text = Txt;
wrdDoc.CheckSpelling(ref oMissing,ref oMissing,ref oMissing,ref
oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref
oMissing,ref oMissing,ref oMissing,ref oMissing);
CheckedTxt = wrdDoc.Content.Text;

wrdApp.Visible = false;
wrdApp.Resize(iWidth, iHeight);
wrdApp.Move(iLeft,iTop);
wrdDoc.Saved = true;
wrdDoc.Close(ref oFalse, ref oMissing,ref oMissing);
wrdApp.Quit(ref oFalse, ref oMissing, ref oMissing);
Marshal.ReleaseComObject(wrdApp);
}
catch (Exception Ex) {
MessageBox.Show(Ex.Message);
}
return CheckedTxt;
}
 
E

encoded

Or you could just use a custom spell checker component such as
SharpSpell, which doesn't require MS Word to be installed and has many
other benefits.

We're using it from ASP.NET and are very pleased with it, but it was
designed primarily for WinForms.

It even has the squiggly underline real-time spell-check (spell as you
type mode), you can find it here:
http://www.tachyon-labs.com/sharpspell/
 
G

Guest

Scott,

Thanks for sharing your code. The spellchecking works great but it seems
that when dragging word's spellchecking window over my winform application,
my window will not repaint itself. Do anyone have the same problem? I have
used almost the same code in a vb6 application which doesn't have this
problem.

Words spell checker is useless for me as it is now because of this. You can
never read what is underneath it's window.

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top