none
how to CheckSpelling is error to words in More than one textbox As in the following example ? RRS feed

  • Question

  • I want CheckSpelling is error to words in More than one textbox (textbox1 , textbox2 , textbox3 >>>>)

    I'm use code :

    If TextBox1.Text.Length > 0 Then
                Dim wordapp As New Word.Application
                wordapp.Visible = False
                Dim doc As Word.Document = wordapp.Documents.Add()
                Dim range As Word.Range
                range = doc.Range()
                range.Text = TextBox1.Text
                doc.Activate()
                Dim SpellCollection As Word.ProofreadingErrors
                SpellCollection = range.SpellingErrors
                If SpellCollection.Count > 0 Then
                    Me.WindowState = FormWindowState.Minimized
                    doc.CheckSpelling()
                    Dim chars() As Char = {CType(vbCr, Char), CType(vbLf, Char)}
                    TextBox1.Text = doc.Range().Text.Trim(chars)
                    doc.Close(SaveChanges:=False)
                    wordapp.Quit()
                    Me.WindowState = FormWindowState.Normal
                Else
                    MessageBox.Show("no find error!")
                End If


    • Edited by srajmuneer Thursday, October 26, 2017 3:14 PM
    Thursday, October 26, 2017 3:13 PM

Answers

  • Hi srajmuneer,

    If you want to add check spelling for all textbox in the form, you can search the textbox control in the form, then check spell for every Textbox control, like this:

     Dim list As New List(Of String)
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            For Each txt As Control In Me.Controls
                If txt.GetType Is GetType(TextBox) Then
                    If txt.Text.Length > 0 Then
                        Dim wordapp As New Word.Application
                        wordapp.Visible = False
                        Dim doc As Word.Document = wordapp.Documents.Add()
                        Dim range As Word.Range
                        range = doc.Range()
                        range.Text = txt.Text
                        doc.Activate()
                        Dim SpellCollection As Word.ProofreadingErrors
                        SpellCollection = range.SpellingErrors
                        If SpellCollection.Count > 0 Then
                            Dim errormessage As String = txt.Name + " have error message"
                            list.Add(errormessage)
                            'Me.WindowState = FormWindowState.Minimized
                            'doc.CheckSpelling()
                            'Dim chars() As Char = {CType(vbCr, Char), CType(vbLf, Char)}
                            'txt.Text = doc.Range().Text.Trim(chars)
                            'doc.Close(SaveChanges:=False)
                            'wordapp.Quit()
                            'Me.WindowState = FormWindowState.Normal
                        Else
                            MessageBox.Show("no find error!")
                        End If
                    End If
                End If
            Next
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by srajmuneer Tuesday, November 7, 2017 5:35 PM
    Friday, October 27, 2017 3:08 AM
    Moderator

All replies

  • Hi srajmuneer,

    If you want to add check spelling for all textbox in the form, you can search the textbox control in the form, then check spell for every Textbox control, like this:

     Dim list As New List(Of String)
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            For Each txt As Control In Me.Controls
                If txt.GetType Is GetType(TextBox) Then
                    If txt.Text.Length > 0 Then
                        Dim wordapp As New Word.Application
                        wordapp.Visible = False
                        Dim doc As Word.Document = wordapp.Documents.Add()
                        Dim range As Word.Range
                        range = doc.Range()
                        range.Text = txt.Text
                        doc.Activate()
                        Dim SpellCollection As Word.ProofreadingErrors
                        SpellCollection = range.SpellingErrors
                        If SpellCollection.Count > 0 Then
                            Dim errormessage As String = txt.Name + " have error message"
                            list.Add(errormessage)
                            'Me.WindowState = FormWindowState.Minimized
                            'doc.CheckSpelling()
                            'Dim chars() As Char = {CType(vbCr, Char), CType(vbLf, Char)}
                            'txt.Text = doc.Range().Text.Trim(chars)
                            'doc.Close(SaveChanges:=False)
                            'wordapp.Quit()
                            'Me.WindowState = FormWindowState.Normal
                        Else
                            MessageBox.Show("no find error!")
                        End If
                    End If
                End If
            Next
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by srajmuneer Tuesday, November 7, 2017 5:35 PM
    Friday, October 27, 2017 3:08 AM
    Moderator
  • Hi Cherry Bu , I tried your code but it did not work

    What do you mean 'did not work'?  Was there an error?  If so, what was the error message and which line did it occur at?  What were the values of the variable in that line?   Or did it not do what you expected it to do?   If so, what did you expect, and what did you actually get?   Or was there some other problem?
    Friday, October 27, 2017 8:33 PM
  • Hi Acamar I mean If a Error Spelling is found or not found, show message appears ("no find error!") for all textbox Where no results ever appear, Where  when using the old code the results appear show dialog Check Spelling for one textbox



    • Edited by srajmuneer Friday, October 27, 2017 10:00 PM
    Friday, October 27, 2017 9:53 PM
  • Hi Acamar I mean If a Error Spelling is found or not found, show message appears ("no find error!") for all textbox Where no results ever appear, Where  when using the old code the results appear show dialog Check Spelling for one textbox

    Do you mean that the result is not being copied back into the textbox?  That suggests you have not included the line

      txt.Text = doc.Range().Text.Trim(chars)
    and associated supporting code from the example provided.


    Saturday, October 28, 2017 12:12 AM
  • thanks Acamar, I mean :

    If a Error Spelling is found show message ("no find error!")

    also if Error Spelling not found, show message ("no find error!") why ?

    Sunday, October 29, 2017 5:21 AM
  • If a Error Spelling is found show message ("no find error!")
    also if Error Spelling not found, show message ("no find error!") why ?

    Is that what is happening now, or is that what should happen?

    You should insert a breakpoint at:

        If SpellCollection.Count > 0 Then
    and check the value of SpellCollection.Count.   Then step the code through one line at a time and see which lines of code get executed.   You should also append the text box text to the messages, so you know which text box is being examined each time the message appears.

    Sunday, October 29, 2017 7:24 AM
  • thank you very much Cherry Bu for help
    Tuesday, November 7, 2017 5:37 PM