Results 1 to 40 of 101

Thread: Advanced VB/Office Guru™ SpellChecker™

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™ SpellChecker™

    Gangsta Yoda

    Description:
    I wrote this to demonstrate how to take full advantage of MS Word's built in Spelling and Grammer 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/grammer 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 8.0 - 11.0 (97 - 2003)


















    VB Code:
    1. 'In a module (Module1.bas)
    2. Option Explicit
    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. '
    12. 'Early binding:
    13. 'Add a reference to MS Word xx.0 Object Library
    14. 'Modifications: none.
    15.  
    16. 'Late binding:
    17. 'No references needed to any version of Word
    18. 'Modifications: Change object vars definitions (moApp & oDoc) to Object
    19. 'Change constants to their numeric equilivalents.
    20.  
    21. 'Requirements:
    22. 'MS Word version 97 (8.0) - 2003 (11.0)
    23.  
    24. Public moApp As Word.Application
    25. Private mbKillMe As Boolean
    26.  
    27. Public Property Get KillMe() As Boolean
    28.     InitializeMe
    29.     KillMe = mbKillMe
    30. End Property
    31.  
    32. Public Property Let KillMe(Value As Boolean)
    33.     mbKillMe = Value
    34. End Property
    35.  
    36. Public Sub InitializeMe()
    37.     On Error Resume Next
    38.     '<INITIALIZE WORD>
    39.     Set moApp = GetObject(, "Word.Application")
    40.     If TypeName(moApp) <> "Nothing" Then
    41.         Set moApp = GetObject(, "Word.Application")
    42.     Else
    43.         Set moApp = CreateObject("Word.Application")
    44.         mbKillMe = True
    45.     End If
    46. End Sub
    47.  
    48. Public Function SpellMe(ByVal msSpell As String) As String
    49.  
    50.     On Error GoTo No_Bugs
    51.  
    52.     Dim oDoc As Word.Document
    53.     Dim iWSE As Integer
    54.     Dim iWGE As Integer
    55.     Dim sReplace As String
    56.     Dim lResp As Long
    57.  
    58.     If msSpell = vbNullString Then Exit Function
    59.     InitializeMe
    60.     Select Case moApp.Version
    61.         Case "9.0", "10.0", "11.0"
    62.             Set oDoc = moApp.Documents.Add(, , 1, True)
    63.         Case "8.0"
    64.             Set oDoc = moApp.Documents.Add
    65.         Case Else
    66.             MsgBox "Unsupported Version of Word.", vbOKOnly + vbExclamation, "VB/Office Guru™ SpellChecker™"
    67.             Exit Function
    68.     End Select
    69.     Screen.MousePointer = vbHourglass
    70.     App.OleRequestPendingTimeout = 999999
    71.     oDoc.Words.First.InsertBefore msSpell
    72.     iWSE = oDoc.SpellingErrors.Count
    73.     iWGE = oDoc.GrammaticalErrors.Count
    74.     '<CHECK SPELLING AND GRAMMER DIALOG BOX>
    75.     If iWSE > 0 Or iWGE > 0 Then
    76.         '<HIDE MAIN WORD WINDOW>
    77.         moApp.Visible = False
    78.         If (moApp.WindowState = wdWindowStateNormal) Or (moApp.WindowState = wdWindowStateMaximize) Then
    79.             moApp.WindowState = wdWindowStateMinimize
    80.         Else
    81.             moApp.WindowState = wdWindowStateMinimize
    82.         End If
    83.         '</HIDE MAIN WORD WINDOW>
    84.         '<PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
    85.         moApp.Dialogs(wdDialogToolsSpellingAndGrammar).Application.Options.CheckGrammarWithSpelling = True
    86.         moApp.Dialogs(wdDialogToolsSpellingAndGrammar).Application.Options.SuggestSpellingCorrections = True
    87.         moApp.Dialogs(wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreUppercase = True
    88.         moApp.Dialogs(wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreInternetAndFileAddresses = True
    89.         moApp.Dialogs(wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreMixedDigits = False
    90.         moApp.Dialogs(wdDialogToolsSpellingAndGrammar).Application.Options.ShowReadabilityStatistics = False
    91.         '</PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
    92.         '<DO ACTUAL SPELL CHECKING>
    93.         moApp.Visible = True
    94.         moApp.Activate
    95.         lResp = moApp.Dialogs(wdDialogToolsSpellingAndGrammar).Display
    96.         '</DO ACTUAL SPELL CHECKING>
    97.         If lResp < 0 Then
    98.             moApp.Visible = True
    99.             MsgBox "Corrections Being Updated!", vbOKOnly + vbInformation, App.ProductName
    100.             Clipboard.Clear
    101.             oDoc.Select
    102.             oDoc.Range.Copy
    103.             sReplace = Clipboard.GetText(1)
    104.             '<FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
    105.             If (InStrRev(sReplace, Chr(13) & Chr(10))) = (Len(sReplace) - 1) Then
    106.                 sReplace = Mid$(sReplace, 1, Len(sReplace) - 2)
    107.             End If
    108.             '</FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
    109.             SpellMe = sReplace
    110.         ElseIf lResp = 0 Then
    111.             MsgBox "Spelling Corrections Have Been Canceled!", vbOKOnly + vbCritical, "VB/Office Guru™ SpellChecker"
    112.             SpellMe = msSpell
    113.         End If
    114.     Else
    115.         MsgBox "No Spelling Errors Found" & vbNewLine & "Or No Suggestions Available!", vbOKOnly + vbInformation, _
    116.         "VB/Office Guru™ SpellChecker"
    117.         SpellMe = msSpell
    118.     End If
    119.     '</CHECK SPELLING AND GRAMMER DIALOG BOX>
    120.     oDoc.Close False
    121.     Set oDoc = Nothing
    122.     '<HIDE WORD IF THERE ARE NO OTHER INSTANCES>
    123.     If KillMe = True Then
    124.         moApp.Visible = False
    125.     End If
    126.     '</HIDE WORD IF THERE ARE NO OTHER INSTANCES>
    127.     Screen.MousePointer = vbNormal
    128.     Exit Function
    129. No_Bugs:
    130.     If Err.Number = "91" Then
    131.         Resume Next
    132.     ElseIf Err.Number = "462" Then
    133.         MsgBox "Spell Checking Is Temporary Un-Available!" & vbNewLine & "Try Again After Program Re-Start.", _
    134.         vbOKOnly + vbInformation, "ActiveX Server Not Responding"
    135.         Screen.MousePointer = vbNormal
    136.     ElseIf Err.Number = 429 Then
    137.         Set moApp = Nothing
    138.         Resume Next
    139.     Else
    140.         MsgBox Err.Number & " " & Err.Description, vbOKOnly + vbInformation, App.ProductName
    141.         Screen.MousePointer = vbNormal
    142.     End If
    143. End Function
    144.  
    145. '*********************************************************
    146.  
    147. 'Example usage:
    148. 'Behind a form (Form1)
    149. '
    150. 'Add a single or multi-line textbox to your form
    151. 'Add a command button (Command1) to invoke the spell checking.
    152. Option Explicit
    153.  
    154. Private Sub Command1_Click()
    155.     '<SPELL CHECK>
    156.     Text1.Text = SpellMe(Text1.Text)
    157. End Sub
    158.  
    159. Private Sub Form_Load()
    160.     '<CALL THE SPELLME INITIALIZATION PROCEDURE BEFORE ANY USE>
    161.     InitializeMe
    162. End Sub
    163.  
    164. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    165.     If KillMe = True Then
    166.         moApp.Quit False
    167.     End If
    168.     Set moApp = Nothing
    169. End Sub
    Attached Images Attached Images      
    Last edited by RobDog888; Jul 21st, 2005 at 01:19 PM. Reason: Spelling error :lol:; Another spelling error :(, Line Break fix; Hide Word Fix
    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