C# Problem : for spell checking ability in richtextbox
thanks for this lovely article but i need this spell checking coding in C#.NET and im demn new to it.
however, i tried but it raise error at some point .. i think it's quiet easy for u
thanks
Code:
namespace SpellCheck
{
public class CheckAndCorrect
{
Microsoft.Office.Interop.Word._Application MSOfficeApp;
private bool mbKillMe;
protected bool Property KillMe
{
get
{
InitializeMe();
KillMe = mbKillMe;
return KillMe;
}
set
{
mbKillMe = Value;
}
}
public void InitializeMe()
{
try
{
//<INITIALIZE WORD>
MSOfficeApp = DirectCast(GetObject(, "Word.Application"), Word.Application);
}
catch (Exception e)
{
if (TypeName(MSOfficeApp) = "Nothing")
{
MSOfficeApp = DirectCast(CreateObject("Word.Application"), Word.Application);
mbKillMe = True;
}
else
{
MessageBox.Show(ex.Message, "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
protected string SpellMe(string Text)
{
Microsoft.Office.Interop.Word.Document oDoc;
int iWSE,iWGE,iResp;
string sReplace;
if (Text = String.Empty)
{
}
else
{
try
{
InitializeMe();
switch(MSOfficeApp.Version)
{
case "9.0" :
case "10.0" :
case "11.0" :
oDoc = MSOfficeApp.Documents.Add(,,,1,true);
break;
case "8.0" :
oDoc = MSOfficeApp.Documents.Add();
break;
default:
return;
}
oDoc.Words.First.InsertBefore(Text);
iWSE = oDoc.SpellingErrors.Count;
iWGE = oDoc.GrammaticalErrors.Count;
//<CHECK SPELLING AND GRAMMER DIALOG BOX>
if (iWSE > 0 || iWGE > 0)
{
// <HIDE MAIN WORD WINDOW>
MSOfficeApp.Visible = False;
if (MSOfficeApp.WindowState = Word.WdWindowState.wdWindowStateNormal ||MSOfficeApp.WindowState = Word.WdWindowState.wdWindowStateMaximize)
MSOfficeApp.WindowState = Word.WdWindowState.wdWindowStateMinimize;
else
MSOfficeApp.WindowState = Word.WdWindowState.wdWindowStateMinimize;
//</HIDE MAIN WORD WINDOW>
//<PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
MSOfficeApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.CheckGrammarWithSpelling = True;
MSOfficeApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.SuggestSpellingCorrections = True;
MSOfficeApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreUppercase = False;
MSOfficeApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreInternetAndFileAddresses = True;
MSOfficeApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreMixedDigits = False;
MSOfficeApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.ShowReadabilityStatistics = False;
//</PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
//<DO ACTUAL SPELL CHECKING>
MSOfficeApp.Visible = True;
MSOfficeApp.Activate();
iResp = MSOfficeApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Display;
//</DO ACTUAL SPELL CHECKING>
if (iResp < 0 )
{
MSOfficeApp.Visible = True;
MessageBox.Show("Corrections Being Updated!", "VB/Office Guru™ SpellChecker™", MessageBoxButtons.OK, MessageBoxIcon.Information);
oDoc.Select();
oDoc.Range.Copy();
sReplace = DirectCast(Clipboard.GetDataObject.GetData("System.String", True), String);
//<FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
if ((InStrRev(sReplace, Chr(13) & Chr(10))) = (Len(sReplace) - 1))
{
sReplace = String.Mid(sReplace, 1, Len(sReplace) - 2);
}
//</FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
SpellMe = sReplace;
}
else if (iResp = 0 )
{
MessageBox.Show("Spelling Corrections Have Been Canceled!", "VB/Office Guru™ SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
SpellMe = Text;
}
}
else
{
MessageBox.Show("No Spelling Errors Found" & Environment.NewLine & "Or No Suggestions Available!", "VB/Office Guru™ SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
SpellMe = Text;
}
//</CHECK SPELLING AND GRAMMER DIALOG BOX>
oDoc.Close(False);
oDoc = Nothing;
//<HIDE WORD IF THERE ARE NO OTHER INSTANCES>
if (KillMe = True)
{
MSOfficeApp.Visible = False;
}
}
catch (Exception e)
{
MessageBox.Show(ex.Message, "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
}
waiting for quick replay..
Re: C# Problem : for spell checking ability in richtextbox
The quick reply is to use [CODE] tags and also, post it in C#'s bank, not VB's :)
Re: C# Problem : for spell checking ability in richtextbox
Re: C# Problem : for spell checking ability in richtextbox
Another post, you're waiting for a quick reply and you still haven't told us where the issues are. Help us help you. We all have varying amounts of time and enthusiasm. If you point out where the issues are then we don't have to do the tedious job of looking through the code ourselves to try to work where the errors are before we can fix them. Give us as little work to do as possible and you're more likely to get an answer.
I'll tell you one thing at a glance: whenever you are comparing things for equality in C# you use two equals signs. A single equals sign is an assignment operator and has a quite different effect. It probably won't throw an exception but your code won't work as you expect, e.g.
Code:
if (KillMe == True)