J
jarimura
I have a document with several form fields that in turn are assigned
bookmarks (e.g., PatientName), to allow automatic filling of the referenced
fields. This works when the form is protected. However, when I attempted to
use the following spell checking macro to check the contents of the form
fields themselves (adapted from the macro written by Dave Rado, Bill Coan,
and Astrid Zeelenberg on the MVP website,
http://word.mvps.org/faqs/macrosvba/SpellcheckProtectDoc.htm) with the form
protected, I end up LOSING BOTH THE BOOKMARK AND THE FORM FIELD:
Sub RunSpellcheck()
Dim Cancelled As Boolean, MyRange As Range, _
CorrectedError As String, oDoc As Document, _
oSection As Section, OriginalRange As Range
If Documents.Count = 0 Then
Exit Sub
End If
Set oDoc = ActiveDocument
Select Case oDoc.ProtectionType
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
Exit Sub
End Select
Set OriginalRange = Selection.Range
System.Cursor = wdCursorWait
oDoc.Unprotect
oDoc.SpellingChecked = False
StatusBar = "Spellchecking document..."
For Each oSection In oDoc.Sections
If oSection.ProtectedForForms Then
Call CheckProtectedSection(oSection)
If Cancelled Then
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
Exit For
End If
End If
End If
Next oSection
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
System.Cursor = wdCursorNormal
Cancelled = False
CorrectedError = vbNullString
Set MyRange = Nothing
End Sub
Private Sub CheckProtectedSection(oSection As Section)
'Checks ONLY the TEXT formfields but NOT listboxes or checkboxes.
Dim FmFld As FormField, FmFldCount As Long, Pos As Long
Application.ScreenUpdating = False
For Each FmFld In oSection.Range.FormFields
If FmFld.Type = wdFieldFormTextInput Then
If FmFld.TextInput.Type = wdRegularText And FmFld.Enabled Then
If Not Left$(Application.Version, 1) = "8" Then
Call TurnNoProofingOff(FmFld)
End If
FmFld.Range.SpellingChecked = False
FmFld.Range.LanguageID = wdEnglishUS
If FmFld.Range.SpellingErrors.Count > 0 Then
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 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
Cancelled = True
Exit Sub
End If
Else
CorrectedError = MyRange.Text
If Len(CorrectedError) = 0 Then
CorrectedError = MyRange.Words(1).Text
End If
Pos = InStr(CorrectedError, vbTab)
Do While Pos > 0
CorrectedError = Mid$(CorrectedError, Pos + 1)
Pos = InStr(CorrectedError, vbTab)
Loop
Do While Not FmFldCount = _
oSection.Range.FormFields.Count
oDoc.Undo
Loop
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)
Set MyRange = FmFld.Range
FmFld.Range.Fields(1).Unlink
Application.ScreenUpdating = True
MyRange.CheckSpelling
If MyRange.SpellingErrors.Count > 0 Then
Cancelled = True
End If
CorrectedError = MyRange.Text
Do While Not IsObjectValid(FmFld)
oDoc.Undo
Loop
FmFld.Range.Fields(1).Result.Text = CorrectedError
Application.ScreenUpdating = False
End Sub
The article where I obtained the above macro, ends with the statement,
“Because of the way in which Word's spellcheck dialog works, if any of your
formfields are surrounded by (protected) text, some of this text may be
displayed in the dialog alongside a spelling error – thus allowing the user
to modify protected text “through the back doorâ€.
The best way to prevent this is to design your forms such that each
formfield is in its own table cell; and the problem will then never arise.
In my experience, with a little ingenuity, it is always possible to design
your forms in this way.
Failing that, the only ways of completely preventing this problem from
arising would be to have the macro put the result of each formfield, in turn,
into a dummy document (or “spellcheck windowâ€), have the user spellcheck it
there, and have the macro put the spellchecked text back into the formfield's
result. In order for the user to see what they were spellchecking in
context, your macro could tile the form document and the “spellcheck windowâ€.
WOULD ANYONE HELP ME DO JUST THAT?
Thanks.
bookmarks (e.g., PatientName), to allow automatic filling of the referenced
fields. This works when the form is protected. However, when I attempted to
use the following spell checking macro to check the contents of the form
fields themselves (adapted from the macro written by Dave Rado, Bill Coan,
and Astrid Zeelenberg on the MVP website,
http://word.mvps.org/faqs/macrosvba/SpellcheckProtectDoc.htm) with the form
protected, I end up LOSING BOTH THE BOOKMARK AND THE FORM FIELD:
Sub RunSpellcheck()
Dim Cancelled As Boolean, MyRange As Range, _
CorrectedError As String, oDoc As Document, _
oSection As Section, OriginalRange As Range
If Documents.Count = 0 Then
Exit Sub
End If
Set oDoc = ActiveDocument
Select Case oDoc.ProtectionType
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
Exit Sub
End Select
Set OriginalRange = Selection.Range
System.Cursor = wdCursorWait
oDoc.Unprotect
oDoc.SpellingChecked = False
StatusBar = "Spellchecking document..."
For Each oSection In oDoc.Sections
If oSection.ProtectedForForms Then
Call CheckProtectedSection(oSection)
If Cancelled Then
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
Exit For
End If
End If
End If
Next oSection
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
System.Cursor = wdCursorNormal
Cancelled = False
CorrectedError = vbNullString
Set MyRange = Nothing
End Sub
Private Sub CheckProtectedSection(oSection As Section)
'Checks ONLY the TEXT formfields but NOT listboxes or checkboxes.
Dim FmFld As FormField, FmFldCount As Long, Pos As Long
Application.ScreenUpdating = False
For Each FmFld In oSection.Range.FormFields
If FmFld.Type = wdFieldFormTextInput Then
If FmFld.TextInput.Type = wdRegularText And FmFld.Enabled Then
If Not Left$(Application.Version, 1) = "8" Then
Call TurnNoProofingOff(FmFld)
End If
FmFld.Range.SpellingChecked = False
FmFld.Range.LanguageID = wdEnglishUS
If FmFld.Range.SpellingErrors.Count > 0 Then
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 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
Cancelled = True
Exit Sub
End If
Else
CorrectedError = MyRange.Text
If Len(CorrectedError) = 0 Then
CorrectedError = MyRange.Words(1).Text
End If
Pos = InStr(CorrectedError, vbTab)
Do While Pos > 0
CorrectedError = Mid$(CorrectedError, Pos + 1)
Pos = InStr(CorrectedError, vbTab)
Loop
Do While Not FmFldCount = _
oSection.Range.FormFields.Count
oDoc.Undo
Loop
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)
Set MyRange = FmFld.Range
FmFld.Range.Fields(1).Unlink
Application.ScreenUpdating = True
MyRange.CheckSpelling
If MyRange.SpellingErrors.Count > 0 Then
Cancelled = True
End If
CorrectedError = MyRange.Text
Do While Not IsObjectValid(FmFld)
oDoc.Undo
Loop
FmFld.Range.Fields(1).Result.Text = CorrectedError
Application.ScreenUpdating = False
End Sub
The article where I obtained the above macro, ends with the statement,
“Because of the way in which Word's spellcheck dialog works, if any of your
formfields are surrounded by (protected) text, some of this text may be
displayed in the dialog alongside a spelling error – thus allowing the user
to modify protected text “through the back doorâ€.
The best way to prevent this is to design your forms such that each
formfield is in its own table cell; and the problem will then never arise.
In my experience, with a little ingenuity, it is always possible to design
your forms in this way.
Failing that, the only ways of completely preventing this problem from
arising would be to have the macro put the result of each formfield, in turn,
into a dummy document (or “spellcheck windowâ€), have the user spellcheck it
there, and have the macro put the spellchecked text back into the formfield's
result. In order for the user to see what they were spellchecking in
context, your macro could tile the form document and the “spellcheck windowâ€.
WOULD ANYONE HELP ME DO JUST THAT?
Thanks.