Results 1 to 4 of 4

Thread: C# Problem : for spell checking ability in richtextbox

  1. #1

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Question 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..
    Last edited by Hack; Feb 22nd, 2006 at 06:54 AM. Reason: Added [code] [/code] tags for more clarity.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: C# Problem : for spell checking ability in richtextbox

    Moved from CodeBank

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width