Option Explicit On
Option Strict On
'Copyright © 2005 by RobDog888 (VB/Office Guru™). All Rights reserved.
'
'Distribution: You can freely use this code in your own
' applications provided that this copyright
' is left unchanged, but you may not reproduce
' or publish this code on any web site, online
' service, or distribute as source on any
' media without express permission.
Imports Microsoft.Office.Interop
Public Class SpellCheck
Friend moApp As Word.Application
Private mbKillMe As Boolean
Friend Property KillMe() As Boolean
Get
InitializeMe()
KillMe = mbKillMe
End Get
Set(ByVal Value As Boolean)
mbKillMe = Value
End Set
End Property
Friend Sub InitializeMe()
Try
'<INITIALIZE WORD>
moApp = DirectCast(GetObject(, "Word.Application"), Word.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Word.Application"), Word.Application)
mbKillMe = True
Else
MessageBox.Show(ex.Message, "VB/Office Guru™ SpellChecker™.NET", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
End Sub
Friend Function SpellMe(ByVal msSpell As String) As String
Dim oDoc As Word.Document
Dim iWSE As Integer
Dim iWGE As Integer
Dim iResp As Integer
Dim sReplace As String
If msSpell = String.Empty Then Exit Function
Try
InitializeMe()
Select Case moApp.Version
Case "9.0", "10.0", "11.0"
oDoc = moApp.Documents.Add(, , 1, True)
Case "8.0"
oDoc = moApp.Documents.Add
Case Else
MessageBox.Show("Unsupported Version of Word.", "VB/Office Guru™ SpellChecker™.NET", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Function
End Select
oDoc.Words.First.InsertBefore(msSpell)
iWSE = oDoc.SpellingErrors.Count
iWGE = oDoc.GrammaticalErrors.Count
'<CHECK SPELLING AND GRAMMER DIALOG BOX>
If iWSE > 0 Or iWGE > 0 Then
'<HIDE MAIN WORD WINDOW>
moApp.Visible = False
If (moApp.WindowState = Word.WdWindowState.wdWindowStateNormal) Or _
(moApp.WindowState = Word.WdWindowState.wdWindowStateMaximize) Then
moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize
Else
moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize
End If
'</HIDE MAIN WORD WINDOW>
'<PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.CheckGrammarWithSpelling = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.SuggestSpellingCorrections = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreUppercase = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreInternetAndFileAddresses = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreMixedDigits = False
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.ShowReadabilityStatistics = False
'</PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
'<DO ACTUAL SPELL CHECKING>
moApp.Visible = True
moApp.Activate()
iResp = moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Display
'</DO ACTUAL SPELL CHECKING>
If iResp < 0 Then
moApp.Visible = True
MessageBox.Show("Corrections Being Updated!", "VB/Office Guru™ SpellChecker™", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
oDoc.Select()
oDoc.Range.Copy()
sReplace = DirectCast(Clipboard.GetDataObject.GetData("System.String", True), String)
'<FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
If (InStrRev(sReplace, Chr(13) & Chr(10))) = (Len(sReplace) - 1) Then
sReplace = Mid$(sReplace, 1, Len(sReplace) - 2)
End If
'</FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
SpellMe = sReplace
ElseIf iResp = 0 Then
MessageBox.Show("Spelling Corrections Have Been Canceled!", "VB/Office Guru™ SpellChecker™.NET", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
SpellMe = msSpell
End If
Else
MessageBox.Show("No Spelling Errors Found" & Environment.NewLine & "Or No Suggestions Available!", _
"VB/Office Guru™ SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
SpellMe = msSpell
End If
'</CHECK SPELLING AND GRAMMER DIALOG BOX>
oDoc.Close(False)
oDoc = Nothing
'<HIDE WORD IF THERE ARE NO OTHER INSTANCES>
If KillMe = True Then
moApp.Visible = False
End If
'</HIDE WORD IF THERE ARE NO OTHER INSTANCES>
Catch ex As Exception
MessageBox.Show(ex.Message, "Welcome To Programmer's Editor Spell Check", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Function
End Class