Results 1 to 6 of 6
  1. #1
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FormsSpellCheck (Word 2000/SR-1)

    I see that the MVP site has a new spell check routine for forms. However, I noticed that if the correction is such that the form field is destroyed, it seems to hang.

    For example, if your text is something like this:

    Now is the time{for} all...

    where {for} is actually a formfield with the word "for", Word will naturally mark them as incorrect & suggest time {for}. When you accept that correction, you actually destroy the form field.

    Then Word hangs at the line 102 xxiii. which I showed below. I wonder if someone knows how to fix it.

    Thanks in advance,
    -------------------------------------------------------------

    Here is the code (with numbered lines):
    <pre>1. Sub FormsSpellCheck()
    2. '
    3. ' FormsSpellCheck Macro
    4. ' Macro taken from MVP site on 1/5/02
    5. '
    6. Dim Cancelled As Boolean, MyRange As Range, CorrectedError As String
    7. Dim oSection As Section, OriginalRange As Range, oDoc As Document

    8. 'If no documents open, quit macro
    9. If Documents.Count = 0 Then
    10. Exit Sub
    11. End If

    12. Set oDoc = ActiveDocument

    13. 'Check what type of protection - if any - has been applied
    14. Select Case oDoc.ProtectionType

    15. 'If not protected, or if protected for tracked changes,
    16. 'run spellchecker and quit
    17. '-------------
    18. Case wdNoProtection, wdAllowOnlyRevisions
    19. If Options.CheckGrammarWithSpelling Then
    20. oDoc.CheckGrammar
    21. Else
    22. oDoc.CheckSpelling
    23. End If
    24. Application.ScreenRefresh
    25. If oDoc.SpellingErrors.Count = 0 Then
    26. If Options.CheckGrammarWithSpelling Then
    a. MsgBox "The spelling and grammar check is complete", vbInformation
    27. Else
    a. MsgBox "The spelling check is complete", vbInformation
    28. End If
    29. End If
    30. System.Cursor = wdCursorNormal
    31. Exit Sub
    32. '-------------
    33. Case wdAllowOnlyComments
    34. 'Don't want to run spellchecker if protected for comments
    35. Exit Sub
    36. End Select

    37. Set OriginalRange = Selection.Range
    38. System.Cursor = wdCursorWait

    39. '-------------
    40. '-------------
    41. 'If we've got this far, it's protected for forms
    42. 'Now unprotect the document
    43. oDoc.Unprotect
    44. oDoc.SpellingChecked = False

    45. 'Check each section for its protection property -
    46. 'which you can get even afer unprotecting the document.
    47. 'If the section was protected, call a subroutine to spellcheck the formfields
    48. 'if it wasn't, spellcheck the section
    49. StatusBar = "Spellchecking document ..."
    50. For Each oSection In oDoc.Sections
    51. If oSection.ProtectedForForms Then
    52. Call CheckProtectedSection(oSection)
    53. If Cancelled Then
    54. 'Boolean variable returned by CheckProtectedSection
    55. 'procedure if user pressed Cancel buttoon
    56. Exit For
    57. End If
    58. Else
    59. If oSection.Range.SpellingErrors.Count > 0 Then
    60. Application.ScreenUpdating = True
    61. oSection.Range.CheckSpelling
    62. If oSection.Range.SpellingErrors.Count > 0 Then
    a. 'User pressed Cancel button
    b. '(Pressing Ignore reduces the count, pressing Cancel doesn't)
    c. Exit For
    63. End If
    64. End If
    65. End If
    66. Next oSection

    67. 'Re-protect the document
    68. oDoc.Protect Type:=wdAllowOnlyFormFields, noreset:=True
    69. OriginalRange.Select
    70. Application.ScreenRefresh
    71. If oDoc.Range.SpellingErrors.Count = 0 Then
    72. MsgBox "The spelling check is complete", vbInformation
    73. End If

    74. 'Release variables from memory
    75. System.Cursor = wdCursorNormal
    76. Cancelled = False
    77. CorrectedError = vbNullString
    78. Set MyRange = Nothing

    79. End Sub
    80. '--------------------------------------------------------------
    81. Private Sub CheckProtectedSection(oSection As Section)

    82. Dim FmFld As FormField
    83. Dim Cancelled As Boolean, MyRange As Range, CorrectedError As String

    84. 'check only the text formfields,
    85. 'don't check listboxes and checkboxes - this speeds up the code
    86. Application.ScreenUpdating = False
    87. For Each FmFld In oSection.Range.FormFields
    88. 'Check to see if the field is a text formfield
    89. If FmFld.Type = wdFieldFormTextInput Then
    90. 'Check if the field is a 'real' text field (no date, formula etc);
    91. ' and that it is enabled for text input
    92. If FmFld.TextInput.Type = wdRegularText And FmFld.Enabled Then
    93. 'The following subroutine won't be called if Word 97 is in use
    94. If Not Left$(Application.Version, 1) = "8" Then
    a. Call TurnNoProofingOff(FmFld)
    95. End If
    96. FmFld.Range.SpellingChecked = False

    97. 'Change the language constant in the following line if necessary;
    98. 'when you type the = sign, a list of all supported language constants will
    99. 'appear, and you can choose one from the list.
    100. FmFld.Range.LanguageID = wdEnglishUS 'Or whichever is appropriate for you

    101. 'If the current form field contains errors, spellcheck the text in it
    102. If FmFld.Range.SpellingErrors.Count > 0 Then
    a. 'The following condition is to allow for a
    'Word 97 bug, which was fixed in 2000;
    b. 'if the formfield is in a table an contains more than 1 paragraph, then
    c. 'spellchecking it will crash Word 97
    d. If Left$(Application.Version, 1) = "8" _
    i. And FmFld.Range.Paragraphs.Count > 1 _
    ii. And FmFld.Range.Tables.Count > 0 Then
    iii. 'Call Word97TableBugWorkaround(FmFld)
    iv. If Cancelled Then Exit Sub
    e. Else
    i. 'Set a range to the formfield's range in case the user
    ii. 'accidentally destroys the formfield by overtyping its entire contents
    iii. Set MyRange = FmFld.Range
    iv. Application.ScreenUpdating = True
    v. FmFld.Range.CheckSpelling

    vi. If IsObjectValid(FmFld) Then
    vii. If FmFld.Range.SpellingErrors.Count > 0 Then
    viii. 'User pressed Cancel button
    ix. '(Pressing Ignore reduces the count, pressing Cancel doesn't)
    x. Cancelled = True
    xi. Exit Sub
    xii. End If
    xiii. Else
    xiv. 'If formfield was destroyed because the user overtyped its entire contents
    xv. CorrectedError = MyRange.Text
    xvi. If Len(CorrectedError) = 0 Then
    xvii. CorrectedError = MyRange.Words(1).Text
    xviii. End If
    xix. Do While Not IsObjectValid(FmFld)
    xx. 'If formfield was destroyed when the user corrected the spelling,
    xxi. 'reinstate it, and put the user's correction into its result
    xxii. ActiveDocument.Undo
    xxiii. Loop
    xxiv. FmFld.Result = CorrectedError
    xxv. End If
    f. End If
    g. Application.ScreenUpdating = False
    103. End If
    104. End If
    105. End If
    106. Next FmFld

    107. End Sub
    108. '-----------------------------------------------
    109. Private Sub TurnNoProofingOff(FmFld As FormField)
    110. 'This subroutine is called only in Word 2000 and above
    111. FmFld.Range.NoProofing = False
    112. End Sub
    </pre>


  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: FormsSpellCheck (Word 2000/SR-1)

    Phil,

    What is the result you want to end up with in this situation? - Do you want the spelling fixed ("time for") and the formfield destroyed, or do you want the spelling fixed and the formfield restored, but with the required space inserted between "time" and "for"?

    Gary

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FormsSpellCheck (Word 2000/SR-1)

    Hi Gary,
    I'm trying for the latter. i.e. the spelling corrected & the form field restored. In reading the comments in the routine, I thought that's what was intended, but it doesn't work that way.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: FormsSpellCheck (Word 2000/SR-1)

    Hi again,

    I was afraid that was the one you wanted! <img src=/S/devil.gif border=0 alt=devil width=15 height=15>

    Best as I can tell, the code doesn't contemplate that situation, and it looks like catering for that situation might mean throwing out its ability to cater to the originally intended situations! (At least that's what I'm concluding one hour and three application crashes later).

    Seems like you'll need some other, additional method to add spaces before or after any formfields that lack spaces before or aft, before running this special spellcheck.

    Gary

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FormsSpellCheck (Word 2000/SR-1)

    Thanks, Gary. I didn't mean to cause you so much trouble. I guess I'll just have to make sure that all form fields have a space separating them. Sorry you had so many crashes. I tried 3 times also--great minds think alike. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  6. #6
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FormsSpellCheck (Word 2000/SR-1)

    I tried that scenario. Word crashed. And I had forgotten to save the other current work.

    OK, let's make it even worse.

    You have a phrase like "now is the time for {al} good men...".

    The rotine shows you "al" in context. But it shows you the oter words.

    You overtype everything- and click "OK".

    The "protected" area of the form is destroyed.

    Now, there's one "quick and easy" way out. You can put every form field into a table cell. That way, nothing else gets shown in the spell check form (I don't know if this will help the original situation though). So that any corrections only go into the table cell.

    Another way (as suggested by WMVP Dave Rado) is to copy the field into a temp document, and spell check it in isolation, before copying the result back into the original form field.

    Any takers?

    Sorry, I've got too much else just now.

    Get your name up in lights on the WMVPS site?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Posting Permissions

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