locked
Spell Checking for Rich Text Box RRS feed

  • Question

  •  

    Dim objWord As Object

    Dim objDoc As Object

    Dim strResult As String

    Try

    'Create a new instance of word Application

    objWord = CreateObject("word.Application")

    objWord.Visible = False

    Select Case objWord.Version

    'Office 2000

    Case "9.0"

    objDoc = objWord.Documents.Add(, , 1, True)

    'Office XP

    Case "10.0"

    objDoc = objWord.Documents.Add(, , 1, True)

    'Office 97

    Case Else ' Office 97

    objDoc = objWord.Documents.Add

    End Select

    objDoc.Content = RichTextBox.Text

    objDoc.CheckSpelling()

    strResult = objDoc.Content.Text.Substring(0, objDoc.Content.Text.Length - 1)

    If RichTextBox.Text = strResult Then

    objWord.Visible = False

    ' There were no spelling errors, so give the user a

    ' visual signal that something happened

    MessageBox.Show("No misspelled words found.", "eMWChart")

    End If

    'Clean up

    objDoc.Close(False)

    objDoc = Nothing

    objWord.Application.Quit(True)

    objWord = Nothing

    ' Replace the selected text with the corrected text. It's important that

    ' this be done after the "Clean Up" because otherwise there are problems

    ' with the screen not repainting

    RichTextBox.Text = strResult

     

     

    I am having the above lines of code. I am having a small problemn with this. I am using rich textbox control for which i am doing spell checking. So everything is goin well but im missing the rich format i have applied. Into the strResult i am getting the text of the rich text box. so after finishing spell checking  i am assigning value to RichTextBox.Text property. but i dont want to loose my rich format. if iam using RichTextBox.rtf property im not getting the replaced using the spell checker window. Please suggest me how to get both rtf format and spelll checking done. and where i am going wrong.

     

    Regards,

    Monday, October 15, 2007 3:45 PM

Answers

  • Hi Srinivas,

     

    I got around this by only getting the list of mispelled words and the suggested corrections from Word. I then used a search replace type operation to correct my real RTF document.

     

    I found this solution somewhere in MSDN but was not able to find again (not quickly anyway).

     

    Here is the code for my spell check form (frmStatusDiary2 is my form that has my rich text control).

     

    Code Block

    Imports System.Windows.Forms

    Public Class SpellCheck

        Private SpellErrors As ProofreadingErrors
        Private SpellFixes As SpellingSuggestions
        Private WordApp As New Microsoft.Office.Interop.Word.Application
        Private bCanFix As Boolean = False

        Private Sub CheckIt()
            Dim iWord As Integer = 0
            Dim sWord As String
            Dim Range As Word.Range
            labStatus.Text = "Loading... Please wait..."
            WordApp.Documents.Add()
            labStatus.Text = "Checking... Please wait..."
            Range = WordApp.ActiveDocument.Range
            Range.InsertAfter(frmStatusDiary2.rtfEdit.Text)
            SpellErrors = Range.SpellingErrors
            If SpellErrors.Count > 0 Then
                lstErrors.Items.Clear()
                lstFixes.Items.Clear()
                For iWord = 1 To SpellErrors.Count
                    sWord = SpellErrors.Item(iWord).Text
                    If lstErrors.FindStringExact(sWord) < 0 Then
                        lstErrors.Items.Add(sWord)
                    End If
                Next
                labErrors.Text = "Spelling Errors: " & lstErrors.Items.Count.ToString
            Else
                labErrors.Text = "Spelling Errors"
            End If
            labFixes.Text = "Suggestions"
            labStatus.Text = "Check complete with " & _
                SpellErrors.Count.ToString & " errors"
        End Sub

        Private Sub OK_Button_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles OK_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub

        Private Sub SpellCheck_Load( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles MyBase.Load
            'Give the interface a bit of time to draw before doing stuff.
            tmrFoo.Enabled = True
        End Sub

        Private Sub SpellCheck_FormClosing( _
            ByVal sender As System.Object, _
            ByVal e As System.Windows.Forms.FormClosingEventArgs) _
            Handles MyBase.FormClosing
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            WordApp.Quit(False)
        End Sub

        Private Sub tmrFoo_Tick( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles tmrFoo.Tick
            tmrFoo.Enabled = False
            CheckIt()
        End Sub

        Private Sub lstErrors_SelectedIndexChanged( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles lstErrors.SelectedIndexChanged
            Dim iWord As Integer = 0
            btnReplace.Enabled = False
            labStatus.Text = ""
            SpellFixes = WordApp.GetSpellingSuggestions(lstErrors.Text)
            lstFixes.Items.Clear()
            If SpellFixes.Count > 0 Then
                For iWord = 1 To SpellFixes.Count
                    lstFixes.Items.Add(SpellFixes.Item(iWord).Name)
                Next
                bCanFix = True
                labFixes.Text = "Suggestions: " & SpellFixes.Count.ToString
            Else
                lstFixes.Items.Add("No suggestions")
                labFixes.Text = "Suggestions"
                bCanFix = False
            End If
        End Sub

        Private Sub lstFixes_SelectedIndexChanged( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles lstFixes.SelectedIndexChanged
            If lstFixes.Items.Count > 0 Then
                If bCanFix Then
                    btnReplace.Enabled = True
                    labStatus.Text = "Replace " & lstErrors.Text & " with " & lstFixes.Text
                    txtOverride.Text = lstFixes.Text
                Else
                    btnReplace.Enabled = False
                    labStatus.Text = ""
                End If
            End If
        End Sub

        Private Sub btnReplace_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles btnReplace.Click
            Dim iReplaced As Integer = 0
            Dim iPos As Integer = 0
            Dim Index As Integer = 0
            Dim iLen As Integer = 0
            Dim sFind As String
            Dim sReplace As String = 0
            Dim Options As RichTextBoxFinds
            If lstErrors.Text.Length > 0 _
                And (lstFixes.Text.Length > 0 Or txtOverride.Text.Length > 0) Then
                frmStatusDiary2.rtfEdit.SelectionLength = 0
                frmStatusDiary2.rtfEdit.SelectionStart = 0
                Options = RichTextBoxFinds.None
                Options = Options Or RichTextBoxFinds.WholeWord
                Options = Options Or RichTextBoxFinds.MatchCase
                iPos = frmStatusDiary2.rtfEdit.SelectionStart
                sFind = lstErrors.Text
                sReplace = txtOverride.Text
                While Index >= 0
                    Index = frmStatusDiary2.rtfEdit.Find(sFind, iPos, Options)
                    If Index >= 0 Then
                        Try
                            frmStatusDiary2.rtfEdit.SelectedText = sReplace
                        Catch
                        End Try
                        iReplaced += 1
                        iLen = frmStatusDiary2.rtfEdit.SelectionLength
                        frmStatusDiary2.rtfEdit.SelectionLength = 0
                        iPos = frmStatusDiary2.rtfEdit.SelectionStart + iLen
                        frmStatusDiary2.rtfEdit.SelectionStart = iPos
                    End If
                End While
                Try
                    lstErrors.Items.Remove(lstErrors.Items.Item(lstErrors.SelectedIndex))
                    labErrors.Text = "Spelling Errors: " & lstErrors.Items.Count.ToString
                Catch
                End Try
                lstErrors.Refresh()
                lstFixes.Items.Clear()
                txtOverride.Text = ""
                labFixes.Text = "Suggestions"
                If iReplaced = 0 Then
                    labStatus.Text = "No words were corrected."
                ElseIf iReplaced = 1 Then
                    labStatus.Text = iReplaced.ToString & " word was corrected."
                Else
                    labStatus.Text = iReplaced.ToString & " words were corrected."
                End If
            End If
            btnReplace.Enabled = False
        End Sub

        Private Sub txtOverride_TextChanged( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles txtOverride.TextChanged
            If txtOverride.Text.Length > 0 Then
                btnReplace.Enabled = True
            End If
        End Sub
    End Class

     

     


    Monday, October 15, 2007 5:13 PM