Spell check form fields

  • Thread starter Thread starter ~~~AAA~~~
  • Start date Start date
A

~~~AAA~~~

Using Word XP I have created a one-page protected form document
template. This document consists of fields for date, time,
location,case number etc. and then a field where an incident narrative
is to be detailed. I want to make the document check the spelling of
these fields.

I would like to be able to disseminate this document to various members
of our police department and have them simply load the document template
into their computer and then create a report and be able to easily check
the spelling of the information they entered into the fields.

Is this possible?

Thank you.
 
Thanks, after some tinkering I was able to get it to work. One thing
though, when it finished spell checking and after making corrections or
not, it does not indicate that it is done as with the regular spell
check. Can I get this to happen?

Thank you.
 
Not sure why you didn't get the "Spell checker is complete" message box
because it is in the code. Unfortunately, I didn't write the code.
Hopefully the creator(s) of this tool will pop in. Not sure if there are
any bugs associated with Word XP. I haven't used this tool since Word 2000
(haven't had a need yet).

Good Luck!

Bill Foley
www.pttinc.com
 
I noticed that when the document is UNPROTECTED the spellchecker macro
works fine and does say at the end that the spell check is complete.
But when I PROTECT the document I do not get the message at the end?
Does this information point to a solution?
 
What did you 'tinker' with exactly? You should be able to copy/paste
all of the macros and aside from deleting the dashed lines (if you
copied them) or modifying the code to include a password then
additional modifications are unnecessary.

--
Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
The macro on the MVP FAQ page works in Word 97-2003 and does display the
message you are requesting. If you modified it, post your code (in the
vba.beginners group) and someone may be able to help you with it. It would
help if you annotate any modifications that you made.
--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
Sorry, when I said that I tinkered with it I meant the dashes, not the
code. I did copy and paste the code from the MVP page in its entirety
and did have to remove the dashes. Then it worked but does not give the
message at the end that the check is complete. If it helps, I pasted
the entire code below.

Thanks again!




Option Explicit

Dim Cancelled As Boolean, MyRange As Range, _
CorrectedError As String, oDoc As Document

Sub RunSpellcheck()

Dim oSection As Section, OriginalRange As Range

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

Set oDoc = ActiveDocument

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

'If not protected, or if protected for tracked changes,
'run spellchecker and quit
'-------------
Case wdNoProtection, wdAllowOnlyRevisions
If Options.CheckGrammarWithSpelling Then
oDoc.CheckGrammar
Else
oDoc.CheckSpelling
End If
Application.ScreenUpdating = True
Application.ScreenRefresh
If oDoc.SpellingErrors.Count = 0 Then
If Options.CheckGrammarWithSpelling Then
MsgBox "The spelling and grammar check is complete", _
vbInformation
Else
MsgBox "The spelling check is complete", vbInformation
End If
End If
System.Cursor = wdCursorNormal
Exit Sub
'-------------
Case wdAllowOnlyComments
'Don't want to run spellchecker if protected for comments
Exit Sub
End Select

Set OriginalRange = Selection.Range
System.Cursor = wdCursorWait

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

'Check each section for its protection property -
'which you can get even after unprotecting the document.
'If the section was protected, call a subroutine to spellcheck the
formfields.
'if it wasn't, spellcheck the section
StatusBar = "Spellchecking document ..."
For Each oSection In oDoc.Sections
If oSection.ProtectedForForms Then
Call CheckProtectedSection(oSection)
If Cancelled Then
'Boolean variable returned by CheckProtectedSection
'procedure if user pressed Cancel button
Exit For
End If
Else
If oSection.Range.SpellingErrors.Count > 0 Then
Application.ScreenUpdating = True
oSection.Range.CheckSpelling
If oSection.Range.SpellingErrors.Count > 0 Then
'User pressed Cancel button
'(Pressing Ignore reduces the count, pressing Cancel
doesn't)
Exit For
End If
End If
End If
Next oSection

'Re-protect the document
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
OriginalRange.Select
Application.ScreenUpdating = True
Application.ScreenRefresh
If oDoc.Range.SpellingErrors.Count = 0 Then
If Options.CheckGrammarWithSpelling Then
MsgBox "The spelling and grammar check is complete", _
vbInformation
Else
MsgBox "The spelling check is complete", vbInformation
End If
End If

'Release variables from memory
System.Cursor = wdCursorNormal
Cancelled = False
CorrectedError = vbNullString
Set MyRange = Nothing

End Sub




Private Sub CheckProtectedSection(oSection As Section)

Dim FmFld As FormField, FmFldCount As Long, Pos As Long

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

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

'If the current form field contains errors, spellcheck the
text in it
If FmFld.Range.SpellingErrors.Count > 0 Then
'The following condition is to allow for a Word 97 bug,
which
'was fixed in 2000; (and in the latest Word 97 patches). If
'the formfield is in a table and contains more than one
'paragraph, then spellchecking it will crash Word 97
If Left$(Application.Version, 1) = "8" _
And FmFld.Range.Paragraphs.Count > 1 _
And FmFld.Range.Tables.Count > 0 Then
Call Word97TableBugWorkaround(FmFld)
If Cancelled Then Exit Sub
Else
'Set a range to the formfield's range in case the user
'accidentally destroys the formfield by overtyping
its entire
'contents
Set MyRange = FmFld.Range
FmFldCount = oSection.Range.FormFields.Count
Application.ScreenUpdating = True

FmFld.Range.CheckSpelling

If IsObjectValid(FmFld) Then
If FmFld.Range.SpellingErrors.Count > 0 Then
'User pressed Cancel button. (Pressing Ignore
'reduces the count, pressing Cancel doesn't)
Cancelled = True
Exit Sub
End If
Else
'If formfield was destroyed because user
overtyped its
'entire contents
CorrectedError = MyRange.Text
If Len(CorrectedError) = 0 Then
CorrectedError = MyRange.Words(1).Text
End If

'Formfields should really NEVER be preceded by
a tab;
'design your forms so that each formfield is in
its own
'table cell (removing borders as necessary).
However, to
'cater for any legacy forms you may have, the
following
'loop works around the possibility that it might be
'preceded by a tab
Pos = InStr(CorrectedError, vbTab)
Do While Pos > 0
CorrectedError = Mid$(CorrectedError, Pos + 1)
Pos = InStr(CorrectedError, vbTab)
Loop

'If formfield was destroyed when the user
corrected the
'spelling, reinstate it, and put the user's
correction into its
'result. Note that although Undo reinstates the
Formfield
'itself, if the Formfield is preceded by a tab,
It doesn't
'reinstate the FmFld object, hence the need to
do a count
'(although, as previously stated, in a
well-designed form,
'formfields should never be preceded by a tab,
as it's
'better use table cells (removing borders as
necessary).
Do While Not FmFldCount = _
oSection.Range.FormFields.Count
oDoc.Undo
Loop

'Also due to a Word bug, if the formfield is
preceded by a
'tab, the text within the formfield may now be
selected
'without the formfield itself being selected!
'Hence the following convoluted workaround
If Selection.FormFields.Count = 0 Then
Selection.MoveRight unit:=wdCharacter
Selection.MoveLeft unit:=wdCharacter,
Extend:=True
End If
If Not IsObjectValid(FmFld) Then
Set FmFld = Selection.FormFields(1)
End If
FmFld.Result = CorrectedError
End If
End If
Application.ScreenUpdating = False
End If
End If
End If
Next FmFld

End Sub




Private Sub TurnNoProofingOff(FmFld As FormField)
'This subroutine is called only in Word 2000 and above
FmFld.Range.NoProofing = False
End Sub




Private Sub Word97TableBugWorkaround(FmFld As FormField)

'Unlink formfield (convert to text)
Set MyRange = FmFld.Range
FmFld.Range.Fields(1).Unlink
Application.ScreenUpdating = True
MyRange.CheckSpelling
If MyRange.SpellingErrors.Count > 0 Then
'User pressed Cancel button
'(Pressing Ignore reduces the count, pressing Cancel doesn't)
Cancelled = True
End If
CorrectedError = MyRange.Text
'Undo to reinstate the formfield
Do While Not IsObjectValid(FmFld)
oDoc.Undo
Loop
FmFld.Range.Fields(1).Result.Text = CorrectedError
Application.ScreenUpdating = False

End Sub
 
I'm not going to debug your code. What follows is a module that I use, which
I don't think is modified from the website, but may be. It works, it
displays the message using the message box at the end of the first
procedure.

Mine may be an earlier version. It only has one message box command while
yours has a number of them. I haven't worked with this for a couple of years
because it has worked in Word 97, 2000, 2002 & 2003 but seems to be pretty
well documented with comment lines. Hope this helps.

Option Explicit
'Module written by Geoff Whitfield based on work by Dave Rado and others on
the MVPs website
'Spell Checks Protected document, all sections
'Placed into Global Template by Charles Kenyon February 2001
'
Dim rngRangeToCheck As Range



Public Sub SpellCheckFormFields()

Dim frmField As FormField
Dim i As Long
Dim strActiveDocName As String

' Don't spell check if no documents exist
If Application.Documents.Count = 0 Then
Exit Sub
End If

Application.ScreenUpdating = False
' Application.ResetIgnoreAll
ActiveDocument.SpellingChecked = False
ActiveDocument.GrammarChecked = False
strActiveDocName = ActiveDocument.Name

'If document is protected, Unprotect it.
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
For i = 1 To ActiveDocument.Sections.Count
Set rngRangeToCheck = ActiveDocument.Sections(i).Range
If ActiveDocument.Sections(i).ProtectedForForms Then
CheckProtectedSection
Else
CheckRange
End If
Next
Else
Set rngRangeToCheck = ActiveDocument.Range
CheckRange
End If

Application.ScreenUpdating = True

MsgBox "The spelling and grammar check is complete", vbInformation


End Sub
Private Sub CheckProtectedSection()
Dim FmFld As FormField
Dim strCorrectedError As String
Dim rngSection As Range, MyRange As Range
Dim CorrectedError As String, sProtection

' Save Protection Status
sProtection = ActiveDocument.ProtectionType
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If

'check only the text formfields that contain text for spelling,
'don't check listboxes and checkboxes - this speeds up the code

Set rngSection = rngRangeToCheck
For Each FmFld In rngSection.FormFields

'Check to see if the field is a text formfield
If FmFld.Type = wdFieldFormTextInput Then

'Check if the field is a 'real' text field (no date, formula etc)
If FmFld.TextInput.Type = wdRegularText Then

'Application.ScreenUpdating = False
FmFld.Select

'The following bit only runs (or even gets compiled) if Word 2000 is
in use
#If VBA6 Then
Selection.NoProofing = False
#End If

'change the language constant in the following line if necessary;
'when you type the = sign, a list of all supported language
constants will
'appear, and you can choose one from the list.
Selection.LanguageID = wdEnglishUS

'Set a range to the formfield's range in case the user
'accidentally destroys the formfield by overtyping its entire
contents
Set MyRange = Selection.Range

'Spellcheck the text in the current form field
If Selection.Range.SpellingChecked = False Then

'Above condition prevents user seeing the selection skip through
'those formfields which don't contain spelling errors,
'making the macro look a bit tidier to the user
Application.ScreenRefresh
Selection.Range.CheckSpelling

If Not IsObjectValid(FmFld) Then
'If formfield was destroyed, reinstate it
'and put the user's correction into its result
CorrectedError = MyRange.Text
ActiveDocument.Undo
FmFld.Result = CorrectedError
End If

End If

End If

End If

Next FmFld
' Finished checking Protected sections
ActiveDocument.Protect Type:=sProtection, noreset:=True

End Sub

Private Sub CheckRange()
rngRangeToCheck.LanguageID = ActiveDocument.AttachedTemplate.LanguageID

rngRangeToCheck.SpellingChecked = False
rngRangeToCheck.GrammarChecked = False
If Options.CheckGrammarWithSpelling Then
rngRangeToCheck.CheckGrammar
Else
rngRangeToCheck.CheckSpelling
End If
End Sub


Public Sub CheckUnprotected()
ActiveDocument.CheckSpelling
End Sub
 
Charles/Beth,
I've been following this thread as I had a similar issue. I've used your code and it works well in my form template. I've added a Spell Checker control to the form to trigger the routine, as the user can't access the macro once the template has been protected. All good so far. My problem is that now that I've added the control to the form template, I can't protect it again....the Protection option is greyed out.... any ideas?

Linda.
 
Would a MacroButton field work?

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Linda said:
Charles/Beth,
I've been following this thread as I had a similar issue. I've used your
code and it works well in my form template. I've added a Spell Checker
control to the form to trigger the routine, as the user can't access the
macro once the template has been protected. All good so far. My problem is
that now that I've added the control to the form template, I can't protect
it again....the Protection option is greyed out.... any ideas?
 
You could run the routine from an exit macro on the last field.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Graham Mayor - Word MVP
E-mail (e-mail address removed)
Web site www.gmayor.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
 
Rather than a control, add a menu item using Tools => Customize or a
macrobutton field or an on-exit macro.
--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

Linda said:
Charles/Beth,
I've been following this thread as I had a similar issue. I've used your
code and it works well in my form template. I've added a Spell Checker
control to the form to trigger the routine, as the user can't access the
macro once the template has been protected. All good so far. My problem is
that now that I've added the control to the form template, I can't protect
it again....the Protection option is greyed out.... any ideas?
 
Back
Top