Spellchecking Locked Bookmarked FormFields Results in LOSS of Book

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.
 
D

Doug Robbins - Word MVP

In spite of the series of exchanges on this subject in the other newsgroup
to which you posted about this issue, you have failed to mention that the
bookmarks are lost ONLY when replacement text is entered into the spell
check dialog WITHOUT taking care to ensure that the bookmark is not
destroyed in the process. If the correction is made by accepting one of the
suggested words, the bookmark and the formfield are NOT lost.

This statement is made more for the benefit of anyone else who may see this
thread so that they fully understand the problem.

The final comments in that article are not really addressing your issue of
the loss of the bookmark when you do NOT take care entering replacement text
into the formfield that is displayed in the spell checking dialog.

I would suggest that if you do NOT want to have to take that care, you make
a note of the error and then after the spell check is complete, return to
the document and correct the spelling in the formfield. In that the macro
turns on the checking of spelling and grammar for the formfields, the
formfield containing errors will then have a red squiggly line under it so
it is easy enough to identify.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

jarimura

Whenever I use the spell checker, I OFTEN run into a situation whereby NONE
of the suggested words is correct. This tends to occur, in particular, such
as in a patient’s proper last name (e.g., Schwarzenegger) or a technical term
(e.g., posterolisthesis), not part of the general dictionary. In that
situation, I would have to manually enter the correction.

Now, again as I mentioned before, if the form field contains a GROUP OF
SEVERAL words, such as would occur in a sentence, then I don’t have a problem
running the macro. HOWEVER, when the form field contains only ONE word
(e.g., Schwarzenegger), then when I try to select the word in the check
dialog, both the bookmarks and the form field are selected. Is there are way
to be careful to select ONLY the misspelled word IN THAT PARTICULAR
SITUATION, without selecting the bookmark and the form field? I am trying to
avoid having to manually look for the error on the document, especially, on a
LARGE document.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top