Spell Check textbox with Word SpellChecker

Strongs
Strongs used Ask the Experts™
on
Hi Experts,

500 points await the expert who can find a solution to this problem. Judging by the posts on various forums, this is quite a common issue, but no solutions seem to work for me.

I am developing in VS2008 with Word 2003 in Vista but the app will be deployed onto Windows XP.

I have a form containing a textbox. The user clicks on a button to spell check the text contained in the textbox.

The text is passed to an instance of Word where the spellchecking takes place. No problems here.

If the instance of Word visibility is set to False, then the app will hang. I guess this is because Word needs to be visible to access the Spellcheck dialog.

If I set the instance of Word visibility to True, in debug mode, the spellcheck dialog will appear on top of the form containing the textbox. In run mode (deployed application) the Word instance sits in the programme tray at the bottom of the window. This would require the user to click on the document to activate the spellcheck. Clearly this would be an unnacceptable requirement for the user.

So, here is my question: How can I force Word to focus the spell check dialog when the user click on the spellcheck button, preferrably with the Word instance visibilty set to False?

I have tried many of the posted solutions on various forums, but nothing seems to work.

Regards,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Forgot to mention that the form containing the textbox is activated with:

fmSpellCheck.ShowDialog()

I guess this makes it a Modal form?
Could be an issue.
Firstly, I would avoid using MS Word to do your spell checking. I use NetSpell (http://www.codeproject.com/KB/string/netspell.aspx) which is easy to implement and easy to use.

Wayne

Commented:
C-API (FindWindow & SetFocus)
"FindWindow" to find the dialog you want set the focus
"SetFocus" to the returned WindowHandle

you can also use "SetForegroundWindow" to force the window come to front

if this does not work:
Find the Window of your TrayIconDialog and send BN_CLICKED or BN_DBLCLICKED, that simulates an User-Event like an single or double Click on the Icon
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
Hi Webtubbs,

Hmm. Looked at this one during my search, but because it is written in C#, i discounted it from my options. Is there a VB version? How easy would it be to convert to VB?

Tauli,

I will give your suggestion a go too. How do you call the API or SetForeGroundWindow for the Word instance?
Doesn't matter what language it is written in. Simply add a reference to the DLL (in the bin directory) and you're away.

Below is the example code from CodeProject converted to VB.Net.

Wayne

Private WithEvents SpellChecker As New NetSpell.SpellChecker.Spelling

Private Sub SpellChecker_DoubledWord(ByVal sender As Object, ByVal args As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker.DoubledWord()
   ' update text
    Me.RichTextbox1.Text = SpellChecker.Text
End Sub

Private Sub SpellChecker_EndOfText(ByVal sender As Object, ByVal e as System.EventArgs) Handles SpellChecker.EndOfText()
    ' update text
    Me.RichTextbox1.Text = SpellChecker.Text
End Sub

Private Sub SpellChecker_MisspelledWord(ByVal sender As Object, ByVal args As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker.MisspelledWord()
    ' update text
    Me.RichTextbox1.Text = SpellChecker.Text
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click()
    SpellChecker.Text = Me.RichTextbox1.Text
    SpellChecker.SpellCheck()
End Sub

Open in new window

Author

Commented:
Hi Webtubbs,

What a cracking little app! Works great. I have lots of textboxes to check, so I am trying to add netspell as class and pass the textbox text to it rather than have the code in each and every textbox form.

I have the following code to instantiate and call the class and the code below that is the class itself.
everything works ok, but the text is not updating. Seems these subs are not firing as control passes back to the form when the class is called. Any ideas?

regards,
'Calling code from form
Dim spClass As New clSpellCheck

        Try
            'check spelling in text box
            If Me.txtEnlarge.Text = Nothing Then
                MessageBox.Show("No text to spell check!")
                Exit Sub
            Else

                spClass.SpellMe(Me.txtEnlarge.Text)
                Dim chars As Char() = {CType(vbCr, Char), CType(vbLf, Char)}
                If spClass.myStr = Nothing Then
                    'do nothing 
                Else
                    Me.txtEnlarge.Text = spClass.myStr.Trim(chars)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try



'Class containing NetSpell
Public Class clSpellCheck
    Private WithEvents SpellChecker As New NetSpell.SpellChecker.Spelling
    Public myStr As String

    Public Sub SpellMe(ByVal sendTxt As String)
        SpellChecker.Text = sendTxt
        SpellChecker.SpellCheck()
    End Sub

    Private Sub SpellChecker_DoubledWord(ByVal sender As Object, ByVal args As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker.DoubledWord
        ' update text
        myStr = SpellChecker.Text
    End Sub

    Private Sub SpellChecker_EndOfText(ByVal sender As Object, ByVal e As System.EventArgs) Handles SpellChecker.EndOfText
        ' update text
        myStr = SpellChecker.Text
    End Sub

    Private Sub SpellChecker_MisspelledWord(ByVal sender As Object, ByVal args As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker.MisspelledWord
        ' update text
        myStr = SpellChecker.Text
    End Sub

End Class

Open in new window

Try the below instead....
        'Calling code from form
        Dim spClass As New clSpellCheck

        Try
            'check spelling in text box
            If Me.txtEnlarge.Text = Nothing Then
                MessageBox.Show("No text to spell check!")
                Exit Sub
            Else
                spClass.SpellMe(Me.txtEnlarge)
                Dim chars As Char() = {CType(vbCr, Char), CType(vbLf, Char)}
                Me.txtEnlarge.Text = Me.txtEnlarge.Text.Trim(chars)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


'Class containing NetSpell
Public Class clSpellCheck
    Private WithEvents SpellChecker As New NetSpell.SpellChecker.Spelling
    Private myTB As TextBox

    Public Sub SpellMe(ByVal tb As TextBox)
        myTB = tb
        SpellChecker.Text = myTB.Text
        SpellChecker.SpellCheck()
    End Sub

    Private Sub SpellChecker_DoubledWord(ByVal sender As Object, ByVal args As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker.DoubledWord
        ' update text
        myTB.Text = SpellChecker.Text
    End Sub

    Private Sub SpellChecker_EndOfText(ByVal sender As Object, ByVal e As System.EventArgs) Handles SpellChecker.EndOfText
        ' update text
        myTB.Text = SpellChecker.Text
    End Sub

    Private Sub SpellChecker_MisspelledWord(ByVal sender As Object, ByVal args As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker.MisspelledWord
        ' update text
        myTB.Text = SpellChecker.Text
    End Sub

End Class

Open in new window

I have used infragisitics in visual studios for spell check many times. Maybe you can find a free olderversion of this on the net. Its simple to use.

Author

Commented:
Thanks for the info scottlafoy,

Got netspell up and running and very happy with the results. Always good though to have another tool in the toolbox.

Webtubbs,

Thanks a million for your assistance in getting netspell up and running.

Author

Commented:
Thank you for your valued assistance in solving this issue.
This is the kind of service that makes using Experts Exchange such a pleasure to use.

Keep up the good work!
>> Thank you for your valued assistance in solving this issue.
>> This is the kind of service that makes using Experts Exchange such a pleasure to use.
>>
>> Keep up the good work!

Thank you for your kind words. It's because of grateful question askers like yourself that I continue offering my assistance :)

Wayne

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial