Click here to Skip to main content
15,887,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
So the spell checker works fine, however there are a few issues.
1.) Does not capitalize the first letter of the alphabet.
2.) Does not underline the error when doing the spelling or grammar check
3.) Most probable the most important the form does not display op top of the application any help with fixing this would be most appreciated.
Thank you.

What I have tried:

' Invokes either the spell or grammar checker.  
    Private Sub SpellOrGrammarCheck(ByVal blnSpellOnly As Boolean)



        Try
            ' Create Word and temporary document objects.
            Dim objWord As Object
            Dim objTempDoc As Object

            ' Declare an IDataObject to hold the data returned from the 
            ' clipboard.
            Dim iData As IDataObject

            ' If there is no data to spell check, then exit sub here.
            If RichTextBox1.Text = "" Then
                Exit Sub
            End If

            objWord = New Word.Application()
            objTempDoc = objWord.Documents.Add
            objWord.Visible = True


            ' Position Word off the screen...this keeps Word invisible 
            ' throughout.
            objWord.WindowState = 0
            objWord.Top = -3000
            Me.Activate()

            ' Copy the contents of the textbox to the clipboard
            Clipboard.SetDataObject(RichTextBox1.Text)

            ' With the temporary document, perform either a spell check or a 
            ' complete
            ' grammar check, based on user selection.
            With objTempDoc
                Dim unused9 = .Content.Paste()
                Dim unused8 = .Activate()
                If blnSpellOnly Then
                    Dim unused7 = .CheckSpelling()

                Else
                    Dim unused6 = .CheckGrammar()
                End If
                ' After user has made changes, use the clipboard to
                ' transfer the contents back to the text box
                Dim unused5 = .Content.Copy()
                iData = Clipboard.GetDataObject
                If iData.GetDataPresent(DataFormats.Text) Then
                    RichTextBox1.Text = CType(iData.GetData(DataFormats.Text),
                        String)
                End If
                .Saved = True
                Dim unused4 = .Close()
            End With
            Show()
            Dim unused3 = objWord.Quit()

            Dim unused2 = MessageBox.Show("The spelling check is complete.",
                "Spell Checker", MessageBoxButtons.OK,
                MessageBoxIcon.Information)

            Dim simpleSound As New SoundPlayer("C:\Users\Tim\Desktop\Backup\Polly_Speaks4U_2_1_1\Resources\docsave2.wav")

            simpleSound.Play()

            ' Microsoft Word must be installed. 
        Catch COMExcep As COMException
            Dim unused1 = MessageBox.Show(
                "Microsoft Word must be installed for Spell/Grammar Check " _
                & "to run.", "Spell Checker")

        Catch Excep As Exception
            Dim unused = MessageBox.Show("An error has occured.", "Spell Checker")




        End Try


    End Sub
Posted
Updated 16-Jun-23 2:43am
v2
Comments
OriginalGriff 16-Jun-23 3:27am    
And?
What have you tried to fix this?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.

1 solution

1. To capitalize, add the following line of code just before you copy your text to the clipboard (Uppercase Letter[^]) -
RichTextBox1.Text = Char.ToUpper(RichTextBox1.Text(0)) + RichTextBox1.Text.Substring(1)


2. To underline the errors, you need to modify your code that checks the spelling or grammar. Replace your lines that perform your spell or grammar check with the following code (ProofreadingErrors object (Word)[^]) -
Dim errors As Word.ProofreadingErrors
If blnSpellOnly Then
    errors = .SpellingErrors
Else
    errors = .GrammarErrors
End If

For Each errorRange As Word.Range In errors
    errorRange.Underline = Word.WdUnderline.wdUnderlineSingle
Next

Make sure to add a reference to Microsoft Word object library. To add the reference, go to 'Project', select 'References' and select 'Microsoft Word'.

3. To display your form on top of any other forms as topmost, add this line of code before calling your '.Show()' (How to keep a Form on top of others[^]) -
TopMost = True


Your combined code will then look like the following (I have not tested the code but the links supplied will be your guide) -
Private Sub SpellOrGrammarCheck(ByVal blnSpellOnly As Boolean)
    Try
        ' Create Word and temporary document objects.
        Dim objWord As Object
        Dim objTempDoc As Object

        ' Declare an IDataObject to hold the data returned from the 
        ' clipboard.
        Dim iData As IDataObject

        ' If there is no data to spell check, then exit sub here.
        If RichTextBox1.Text = "" Then
            Exit Sub
        End If

        objWord = New Word.Application()
        objTempDoc = objWord.Documents.Add
        objWord.Visible = True

        ' Position Word off the screen...this keeps Word invisible 
        ' throughout.
        objWord.WindowState = 0
        objWord.Top = -3000
        Me.Activate()

        ' Capitalize the first letter of the alphabet
        RichTextBox1.Text = Char.ToUpper(RichTextBox1.Text(0)) + RichTextBox1.Text.Substring(1)

        ' Copy the contents of the textbox to the clipboard
        Clipboard.SetDataObject(RichTextBox1.Text)

        ' With the temporary document, perform either a spell check or a 
        ' complete grammar check, based on user selection.
        With objTempDoc
            Dim unused9 = .Content.Paste()
            Dim unused8 = .Activate()

            ' Perform spell or grammar check and underline errors
            Dim errors As Word.ProofreadingErrors
            If blnSpellOnly Then
                errors = .SpellingErrors
            Else
                errors = .GrammarErrors
            End If

            For Each errorRange As Word.Range In errors
                errorRange.Underline = Word.WdUnderline.wdUnderlineSingle
            Next

            ' After the user has made changes, use the clipboard to
            ' transfer the contents back to the text box
            Dim unused5 = .Content.Copy()
            iData = Clipboard.GetDataObject
            If iData.GetDataPresent(DataFormats.Text) Then
                RichTextBox1.Text = CType(iData.GetData(DataFormats.Text), String)
            End If
            .Saved = True
            Dim unused4 = .Close()
        End With

        ' Set the form to topmost
        TopMost = True

        Show()

        Dim unused3 = objWord.Quit()

        Dim unused2 = MessageBox.Show("The spelling check is complete.", "Spell Checker", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Dim simpleSound As New SoundPlayer("C:\Users\Tim\Desktop\Backup\Polly_Speaks4U_2_1_1\Resources\docsave2.wav")
        simpleSound.Play()

        ' Microsoft Word must be installed. 
    Catch COMExcep As COMException
        Dim unused1 = MessageBox.Show("Microsoft Word must be installed for Spell/Grammar Check to run.", "Spell Checker")

    Catch Excep As Exception
        Dim unused = MessageBox.Show("An error has occurred.", "Spell Checker")
    End Try
End Sub
 
Share this answer
 
v2
Comments
Member 11788039 16-Jun-23 8:35am    
Thank you Andre,
The Caps is no working , however when i apply the rest of the code the result is once i have clicked on the spelling or grammar button a message pops up saying the spelling check is complete and there are no spelling errors, ' this is problematic as even if there are errors it is not working correctly.
Andre Oosthuizen 17-Jun-23 6:51am    
Pleasure. Your comment does however not help anything by stating that "it is not working correctly", you need to be specific.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900