locked
Spell check a rich text box with out destroying the formating RRS feed

  • Question

  • After scowering the internet for info on Spell checking the contents of a rich text box I was able to peice together the following code:

     

    private static string checkSpelling(string text)
    
    
    
    {
    
    
    
          string results = "";
    
    
    
    
    
    
    
          Word.Application app = new Word.Application();
    
    
    
          if (text.Length > -0)
    
    
    
          {
    
    
    
                app.Visible = false;
    
    
    
                object template = Missing.Value;
    
    
    
                object newTemplate = Missing.Value;
    
    
    
                object documentType = Missing.Value;
    
    
    
                object visible = false;
    
    
    
                object optional = Missing.Value;
    
    
    
                Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
    
    
    
                doc.Words.First.InsertBefore(text);
    
    
    
                doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional, ref optional,
    
    
    
    ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional);
    
    
    
                object first = 0;
    
    
    
                object last = doc.Characters.Count - 1;
    
    
    
                results = doc.Range(ref first, ref last).Text;
    
    
    
          }
    
    
    
          else
    
    
    
                MessageBox.Show("No text to correct");
    
    
    
    
    
    
    
          object saveChanges = false;
    
    
    
          object originalFormat = Missing.Value;
    
    
    
          object routeDocument = Missing.Value;
    
    
    
          app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
    
    
    
          return results;
    
    
    
    
    When I run this code on a rtb that has formating, it checks the spelling and returns plain text. 
    Does anyone know of a way to do this on the formated text, or preserve the formating?
    }
    Saturday, January 9, 2010 10:40 AM

Answers

All replies

  • BUMP! Realy need help if there is anyone out there that can assist? If anyone knows of another method for spell checking, that doesn't involve thirf party solutions, that would work as well. I just need spell ckeck functionallity.
    Sunday, January 10, 2010 2:08 AM
  • BUMP again.
    Thursday, January 14, 2010 1:36 AM
  • Hi,

    What is the value of the text? could you post more code that can help us to test it?

    You might want to copy the range to clipboard then get it in rtf format and paste it to richtextbox.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, January 14, 2010 4:00 AM
  • Hi,

    A sample about how to copy content of word file to clipboard and retrieve it can be found here:
    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/854170e6-9148-473e-bca4-aa1e6670c984

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Thursday, January 21, 2010 6:18 AM
    Friday, January 15, 2010 2:47 AM
  • Hi, try this code(may be easy) and see if the result has format.

    Function SpellChecker(TextValue)
        
        Dim objWordobject
           Dim objDocobject
         
          Set objWordobject = CreateObject("word.Application")
          objWordobject.WindowState = 2
          objWordobject.Visible = True

          Set objDocobject = objWordobject.Documents.Add( , , 1, True)

          objDocobject.Content=TextValue
          objDocobject.CheckSpelling

        SpellChecker = objDocobject.Content
          objDocobject.Close false
          Set objDocobject = Nothing
          objWordobject.Application.Quit True
          Set objWordobject= Nothing

    End Function


    ___________________________
    April
    My Free Live Chat Software for you!
    Friday, January 15, 2010 9:09 AM
  • Hi, try this code(may be easy) and see if the result has format.

    Function SpellChecker(TextValue)
        
        Dim objWordobject
           Dim objDocobject
         
          Set objWordobject = CreateObject("word.Application")
          objWordobject.WindowState = 2
          objWordobject.Visible = True

          Set objDocobject = objWordobject.Documents.Add( , , 1, True)

          objDocobject.Content=TextValue
          objDocobject.CheckSpelling

        SpellChecker = objDocobject.Content
          objDocobject.Close false
          Set objDocobject = Nothing
          objWordobject.Application.Quit True
          Set objWordobject= Nothing

    End Function

    ____________________________
    April
    My Free Live Chat for you!
    Monday, January 18, 2010 3:26 AM
  • The code Im using is the same as above.
    An example of the text that is in the Rich Text Box is:

    This is a test of spell check and formatting

    This is a test of spell check and formatting

    This is a test of spell check and formatting

    This is a test of spell check and formatting


    When I run this though spell check as RTF, I get all the formatting tags and spell check freeks out.
    When I run just the plain text, the formatting gets lost except for the fist part. In this case BOLD. All 4 lines end up in BOLD font.
    Assuming Underline was used first, then all of the text would be Underlined.

    As far as copying to the clipboard, Im not sure how to intigrate that into what Im using. I hate to ask, but if you could include some code, that would help very much.

    Thanks
    Jim
    Thursday, January 28, 2010 9:46 AM
  • Hi,

    If you open the link in my previous post , you can find the code to put the data to clipboard and retrieve it.
    Then you can try to incorporate it into your project.

    In you first code , you just return a string , but did not show us how do you put it onto a richtext box , so that we can not reproduce your scenario.

    You might need to be more specific about the problem with more code you are working with and we can test it for you.

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, January 29, 2010 2:02 AM
  • Thanks Harry.
    I am using rtbSolution.Text = checkSpelling(rtbSolution.Text);

    This is in an OnClick event for the Spell Check button.

    I've also tried rtbSolution.Rtf = checkSpelling(rtbSolution.Rtf);

    When I pass the Rtf, spell check reads the RTF tags as well as the text.
    When I pass the Text, the return is plain text and wipes out the formating that was added to the rich text box.

    Here is all of the code concerning the spell check:

    private void toolStripButton5_Click(object sender, EventArgs e)
    {
         // run spell check
         rtbSolution.Text = SpellCheck(rtbSolution.Text);
    }
    
    
    private string SpellCheck(string rtf)
    {
         Word._Application app = new Word.Application();
         string results = "";
    
         if (rtf.Length > -0)
         {
              app.Visible = false;
              object template = Missing.Value;
              object newTemplate = Missing.Value;
              object documentType = Missing.Value;
              object visible = false;
              object optional = Missing.Value;
              Word.Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
              doc.Words.First.InsertBefore(rtf);
              doc.FormattingShowClear = true;
    
              doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional, ref optional,
                        ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional);
    
              object first = 0;
              object last = doc.Characters.Count - 1;
              results = doc.Range(ref first, ref last).Text;
         }
    else
         MessageBox.Show("No text to correct");
    
         object saveChanges = false;
         object originalFormat = Missing.Value;
         object routeDocument = Missing.Value;
         app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
         return results;
    }









    private void toolStripButton1_Click(object sender, EventArgs e) { // set bold formating if (rtbSolution.SelectionFont.Bold == true) rtbSolution.SelectionFont = new System.Drawing.Font(rtbSolution.SelectionFont, FontStyle.Regular); else rtbSolution.SelectionFont = new System.Drawing.Font(rtbSolution.SelectionFont, FontStyle.Bold); rtbSolution.Focus(); } private void toolStripButton2_Click(object sender, EventArgs e) { // set underline formatting if (rtbSolution.SelectionFont.Underline == true) rtbSolution.SelectionFont = new System.Drawing.Font(rtbSolution.SelectionFont, FontStyle.Regular); else rtbSolution.SelectionFont = new System.Drawing.Font(rtbSolution.SelectionFont, FontStyle.Underline); rtbSolution.Focus(); } private void toolStripButton3_Click(object sender, EventArgs e) { // set italic formatting if (rtbSolution.SelectionFont.Italic == true) rtbSolution.SelectionFont = new System.Drawing.Font(rtbSolution.SelectionFont, FontStyle.Regular); else rtbSolution.SelectionFont = new System.Drawing.Font(rtbSolution.SelectionFont, FontStyle.Italic); rtbSolution.Focus(); } private void toolStripButton4_Click(object sender, EventArgs e) { // set highlight if (rtbSolution.SelectionColor == Color.Red) rtbSolution.SelectionColor = Color.Black; else rtbSolution.SelectionColor = Color.Red; rtbSolution.Focus(); }
    This is the code for setting the formatting.
    Friday, January 29, 2010 2:37 AM
  • Sorry for the unsitely code.
    Friday, January 29, 2010 2:39 AM