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