?
Solved

having issues using words spell checker vb.net

Posted on 2006-04-19
7
Medium Priority
?
941 Views
Last Modified: 2008-02-26
Hello experts i have referenced microsoft words spell checker in my vb.net app.  My problem is that when I use the spell check feature any change I make to a selected spelling, microsoft word quickly pops up then closes.  It is very noticeable and I was wondering if there is a way to keep it from popping up or making it less noticeable.  Else is there a free spell checker tool that I can use within my vb.net app?  My code is below.

 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 txtpolicy.Text = "" Then
                MessageBox.Show("Please enter some text")
                Exit Sub
            End If

            objWord = New Word.Application
            objTempDoc = objWord.Documents.Add
            objWord.Visible = False

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

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

            ' With the temporary document, perform either a spell check or a
            ' complete
            ' grammar check, based on user selection.
            With objTempDoc
                .Content.Paste()
                .Activate()
                If blnSpellOnly Then
                    .CheckSpelling()
                Else
                    .CheckGrammar()
                End If
                ' After user has made changes, use the clipboard to
                ' transfer the contents back to the text box
                .Content.Copy()
                iData = Clipboard.GetDataObject
                If iData.GetDataPresent(DataFormats.Text) Then
                    txtpolicy.Text = CType(iData.GetData(DataFormats.Text), _
                        String)
                End If
                .Saved = True
                .Close()
            End With

            objWord.Quit()

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

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

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

        End Try

    End Sub
0
Comment
Question by:tentavarious
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16489983
objWord.Visible = False

Bob
0
 

Author Comment

by:tentavarious
ID: 16490108
I had it set to false, what happens is the background of word shows up for a split second then disappears.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16490124
Yeah, I missed that one.  :(

What line causes the background to show up?

Bob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tentavarious
ID: 16490169
Ok, after I ran my program a cupple times it seems to disappear faster now, but it still shows up in the taskbar, anyway to stop it?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16490188
You didn't answer my question.

Bob
0
 

Author Comment

by:tentavarious
ID: 16490247
Its within this with block, the .checkspelling is where is starts
  With objTempDoc
                .Content.Paste()
                .Activate()
                If blnSpellOnly Then
                    .CheckSpelling()
                Else
                    .CheckGrammar()
                End If
                ' After user has made changes, use the clipboard to
                ' transfer the contents back to the text box
                .Content.Copy()
                iData = Clipboard.GetDataObject
                If iData.GetDataPresent(DataFormats.Text) Then
                    txtpolicy.Text = CType(iData.GetData(DataFormats.Text), _
                        String)
                End If
                .Saved = True
                .Close()
            End With
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 16524392
Here is what I came up with:

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop

<Flags()> _
Public Enum SpellCheck
  Spelling = &H1
  Grammar = &H2
  Spelling_Grammer = Spelling Or Grammar
End Enum

Public Class WordSpellChecker

  Public Shared Function DoCheck(ByVal text As String, ByVal mode As SpellCheck) As String

    Dim app As Word.Application
    Dim doc As Word.Document

    Try


      ' If there is no data to spell check, then exit sub here.
      If text.Length = 0 Then
        MessageBox.Show("Please enter some text")
        Exit Function
      End If

      ' Create Word and temporary document objects.
      app = New Word.Application
      doc = app.Documents.Add()

      app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
      app.Visible = False

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

      ' With the temporary document, perform either a spell check or a
      ' spell check/grammar check, based on user selection.
      doc.Content.Paste()
      doc.Activate()

      Select Case mode

        ' Grammar?
        Case SpellCheck.Grammar
          doc.CheckGrammar()

          ' Spelling?
        Case SpellCheck.Spelling
          doc.CheckSpelling()

          ' Spelling/Grammar?
        Case SpellCheck.Spelling_Grammer
          doc.CheckGrammar()
          doc.CheckSpelling()
      End Select       'mode

      app.Visible = False

      ' After user has made changes, use the clipboard to
      ' transfer the contents back to the text box
      doc.Content.Copy()

      ' Declare an IDataObject to hold the data returned from the
      ' clipboard.
      Dim iData As IDataObject = Clipboard.GetDataObject
      If iData.GetDataPresent(DataFormats.Text) Then
        text = iData.GetData(DataFormats.Text).ToString()
      End If

      doc.Close(saveChanges:=False)

      app.Quit()

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

    Catch ex As COMException

      ' Microsoft Word must be installed.
      MessageBox.Show( _
         "Microsoft Word must be installed for Spell/Grammar Check to run.", _
        "Spell Checker")

    Catch ex As Exception

      MessageBox.Show("An error has occured.", "Spell Checker")

    Finally

      If Not app Is Nothing Then
        Marshal.ReleaseComObject(app)
      End If

      If Not doc Is Nothing Then
        Marshal.ReleaseComObject(doc)
      End If

      GC.Collect()

    End Try

    Return text

  End Function  'SpellGrammarCheck

End Class

Example usage:
  Me.txtPolicy.Text = WordSpellChecker(Me.txtPolicy.Text, SpellCheck.Spelling)

Bob
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question