469,960 Members | 2,656 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,960 developers. It's quick & easy.

C# WinForm Spell Checking

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

}
Nov 16 '05 #1
3 3767
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;
}


Sc***********@hq.doe.gov wrote:
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
Vish wrote:
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";
}

}


Nov 16 '05 #2
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/

Nov 16 '05 #3
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?
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Lloyd Dupont | last post: by
12 posts views Thread by Ryan | last post: by
6 posts views Thread by Neil | last post: by
5 posts views Thread by Dean Slindee | last post: by
22 posts views Thread by SmokeWilliams | last post: by
1 post views Thread by SpaceMarine | last post: by
4 posts views Thread by BillE | last post: by
reply views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.