Results 1 to 40 of 65

Thread: Advanced VB/Office Guru™ Word SpellChecker™.NET

Threaded View

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Arrow Advanced VB/Office Guru™ Word SpellChecker™.NET

    Gangsta Yoda™ ®

    Description:
    I wrote this to demonstrate how to take full advantage of MS Word's built in Spelling and Grammar checker. There
    are a few other code examples that show how to do spell checking with Word but they are all using the same technique that
    seems to have a few issues like showing the spell dialog behind your app, messing up the line breaks so you need to
    apply a code fix to restore them, flashing window, etc.

    What makes my spell checker different is that I use the actual spell/grammar checking dialog window and not invoking it by
    coding the usual - "Document.CheckSpelling" which seems to cause the mentioned issues.

    Since I use the dialog window I preset the type of spelling I wish to perform. Now you can make it dynamic by having a
    spell options form in your app to allow the user to preset it to either Ignore words in UPPERCASE, urls, mixed words and
    numbers, check grammer, and whether to show spelling suggestions, etc.

    Requirements:
    Microsoft Word
    Versions 10.0 - 11.0 (2002 - 2003)

    Note: I converted my VB6 version to VB.NET 2003. The VB6 version can be located here.
















    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Copyright © 2005 by RobDog888 (VB/Office Guru™). All Rights reserved.
    4. '
    5. 'Distribution: You can freely use this code in your own
    6. '              applications provided that this copyright
    7. '              is left unchanged, but you may not reproduce
    8. '              or publish this code on any web site, online
    9. '              service, or distribute as source on any
    10. '              media without express permission.
    11. Imports Microsoft.Office.Interop
    12.  
    13. Public Class clsSpellMe
    14.  
    15.     Friend moApp As Word.Application
    16.     Private mbKillMe As Boolean
    17.  
    18.     Friend Property KillMe() As Boolean
    19.         Get
    20.             InitializeMe()
    21.             KillMe = mbKillMe
    22.         End Get
    23.         Set(ByVal Value As Boolean)
    24.             mbKillMe = Value
    25.         End Set
    26.     End Property
    27.  
    28.     Friend Sub InitializeMe()
    29.         Try
    30.             '<INITIALIZE WORD>
    31.             moApp = DirectCast(GetObject(, "Word.Application"), Word.Application)
    32.         Catch ex As Exception
    33.             If TypeName(moApp) = "Nothing" Then
    34.                 moApp = DirectCast(CreateObject("Word.Application"), Word.Application)
    35.                 mbKillMe = True
    36.             Else
    37.                 MessageBox.Show(ex.Message, "VB/Office Guru™ SpellChecker™.NET", _
    38.                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    39.             End If
    40.         End Try
    41.     End Sub
    42.  
    43.     Friend Function SpellMe(ByVal msSpell As String) As String
    44.  
    45.         Dim oDoc As Word.Document
    46.         Dim iWSE As Integer
    47.         Dim iWGE As Integer
    48.         Dim iResp As Integer
    49.         Dim sReplace As String
    50.  
    51.         If msSpell = String.Empty Then Exit Function
    52.         Try
    53.             InitializeMe()
    54.             Select Case moApp.Version
    55.                 Case "9.0", "10.0", "11.0"
    56.                     oDoc = moApp.Documents.Add(, , 1, True)
    57.                 Case "8.0"
    58.                     oDoc = moApp.Documents.Add
    59.                 Case Else
    60.                     MessageBox.Show("Unsupported Version of Word.", "VB/Office Guru™ SpellChecker™.NET", _
    61.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    62.                     Exit Function
    63.             End Select
    64.             oDoc.Words.First.InsertBefore(msSpell)
    65.             iWSE = oDoc.SpellingErrors.Count
    66.             iWGE = oDoc.GrammaticalErrors.Count
    67.             '<CHECK SPELLING AND GRAMMER DIALOG BOX>
    68.             If iWSE > 0 Or iWGE > 0 Then
    69.                 '<HIDE MAIN WORD WINDOW>
    70.                 moApp.Visible = False
    71.                 If (moApp.WindowState = moApp.WindowState.wdWindowStateNormal) Or _
    72.                     (moApp.WindowState = moApp.WindowState.wdWindowStateMaximize) Then
    73.                     moApp.WindowState = moApp.WindowState.wdWindowStateMinimize
    74.                 Else
    75.                     moApp.WindowState = moApp.WindowState.wdWindowStateMinimize
    76.                 End If
    77.                 '</HIDE MAIN WORD WINDOW>
    78.                 '<PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
    79.                 moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.CheckGrammarWithSpelling = True
    80.                 moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.SuggestSpellingCorrections = True
    81.                 moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreUppercase = True
    82.                 moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreInternetAndFileAddresses = True
    83.                 moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreMixedDigits = False
    84.                 moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.ShowReadabilityStatistics = False
    85.                 '</PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
    86.                 '<DO ACTUAL SPELL CHECKING>
    87.                 moApp.Visible = True
    88.                 moApp.Activate()
    89.                 iResp = moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Display
    90.                 '</DO ACTUAL SPELL CHECKING>
    91.                 If iResp < 0 Then
    92.                     moApp.Visible = True
    93.                     MessageBox.Show("Corrections Being Updated!", "VB/Office Guru™ SpellChecker™", _
    94.                     MessageBoxButtons.OK, MessageBoxIcon.Information)
    95.                     oDoc.Select()
    96.                     oDoc.Range.Copy()
    97.                     sReplace = DirectCast(Clipboard.GetDataObject.GetData("System.String", True), String)
    98.                     '<FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
    99.                     If (InStrRev(sReplace, Chr(13) & Chr(10))) = (Len(sReplace) - 1) Then
    100.                         sReplace = Mid$(sReplace, 1, Len(sReplace) - 2)
    101.                     End If
    102.                     '</FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
    103.                     SpellMe = sReplace
    104.                 ElseIf iResp = 0 Then
    105.                     MessageBox.Show("Spelling Corrections Have Been Canceled!", "VB/Office Guru™ SpellChecker™.NET", _
    106.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    107.                     SpellMe = msSpell
    108.                 End If
    109.             Else
    110.                 MessageBox.Show("No Spelling Errors Found" & Environment.NewLine & "Or No Suggestions Available!", _
    111.                 "VB/Office Guru™ SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    112.                 SpellMe = msSpell
    113.             End If
    114.             '</CHECK SPELLING AND GRAMMER DIALOG BOX>
    115.             oDoc.Close(False)
    116.             oDoc = Nothing
    117.             '<HIDE WORD IF THERE ARE NO OTHER INSTANCES>
    118.             If KillMe = True Then
    119.                 moApp.Visible = False
    120.             End If
    121.             '</HIDE WORD IF THERE ARE NO OTHER INSTANCES>
    122.         Catch ex As Exception
    123.             MessageBox.Show(ex.Message, "VB/Office Guru™ SpellChecker™.NET", _
    124.             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    125.         End Try
    126.  
    127.     End Function
    128.  
    129. End Class
    Attached Images Attached Images      
    Last edited by RobDog888; Sep 13th, 2005 at 08:40 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width