Preventing editting of protected text when spell checking form fields

J

jumpingsheep

In reference to the end of the following page:
http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm

"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 only
ways of
completely preventing this problem from arising would either be to:

a) 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".
But this
workaround would be clunky.
b) Write your own spellcheck dialog (using a UserForm).


"Both the above solutions are beyond the scope of this site..."

Both solutions are a bit beyond the scope of my VBA coding abilities
and I am not able to put all my form fields (of which there are many)
into table cells. I was wondering if anyone had possibly implemented
these and what success they'd had?

Thank you
Travis Dent
 
D

Doug Robbins - Word MVP

The following macro will handle a document in which the formfields are only
Text FormFields. With a bit more work, it could be made to handle a
document with other types of formfields

Dim source As Document, target As Document
Dim rtable As Table
Dim i As Long
Dim ffname As Range, ffresult As Range
Set source = ActiveDocument
Set target = Documents.Add
Set rtable = target.Tables.Add(Selection.Range, source.FormFields.Count, 2)
For i = 1 To source.FormFields.Count
rtable.Cell(i, 1).Range = source.FormFields(i).Name
rtable.Cell(i, 2).Range = source.FormFields(i).Result
Next i
For i = 1 To rtable.Rows.Count
rtable.Cell(i, 1).Range.SpellingChecked = True
Next i
target.CheckSpelling
For i = 1 To rtable.Rows.Count
Set ffname = rtable.Cell(i, 1).Range
ffname.End = ffname.End - 1
Set ffresult = rtable.Cell(i, 2).Range
ffresult.End = ffresult.End - 1
source.FormFields(ffname.Text).Result = ffresult
Next i
source.Activate


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

jumpingsheep

Hi Doug, thanks for the reply.

I managed to implement a way of copying the contents of the form field
to another document but had to make use of another instance of Word
which I made invisible. What I am trying to achieve is a user
experience similar to normal spell checking where it will go through a
document and for protected sections, check the text in each form field,
displaying the spell check dialog when an error is found.

Unfortunately with my "copying to another instance of word" solution,
although Word isn't visible, when I close the new instance it is
briefly visible. Here is the method I use for this:

Function CheckSpellink(ByVal strString As String) As Integer
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim dlg As Dialog

' use the hourglass mouse cursor
Dim oldMousePointer As Variant

'oldMousePointer = Screen.MousePointer
'Screen.MousePointer = vbHourglass

Set objWord = New Word.Application

With objWord
.ScreenUpdating = False
.Visible = False
' create a new document, and "paste" the input text
Set objDoc = .Documents.Add
.Selection.Text = strString
OriginalText = strString
' run the check-spelling
Set dlg = .Dialogs(wdDialogToolsSpellingAndGrammar)
dlg.SuggestionListBox = True
dlg.ForegroundGrammar = False
dlg.Show


'.Dialogs(wdDialogToolsSpellingAndGrammar).Show
strString = .Selection.Text

If Selection.Range.SpellingErrors.Count > 0 Then
'User pressed Cancel button. (Pressing Ignore
'reduces the count, pressing Cancel doesn't)
CheckSpellink = 0 'the user pressed cancel
ElseIf strString = OriginalText Then
CheckSpellink = 1 'the user pressed ignore
Else
CheckSpellink = 2 'the user changed the text
End If

' close the document
objDoc.Close wdDoNotSaveChanges
Set objDoc = Nothing
' close the Word instance
.Quit wdDoNotSaveChanges
End With


Set objWord = Nothing
' restore the original couse cursor
'Screen.MousePointer = oldMousePointer

'CheckSpellink = strString
End Function

The other thing I was trying which I thought might work but ends up
crashing Word is this:

The text shown in the spell check dialog includes the paragraph that
the form field is part of. What I thought to do is compare the
protected text in the paragraph before the form field with the same
text after the spell check is performed. If they differ then replace
the text in the document with the original text. It seemed like a
brilliant idea but unfortunately, crash, do you want to report this to
Microsoft, etc... :(
 
J

Jumping Sheep

Hi, found what looks to be a solution. Not too pretty but it should
work:

OldText = ActiveDocument.Range( _
Start:=FmFld.Range.Paragraphs(1).Range.Start, _
End:=FmFld.Range.Start).Text

FmFld.Range.CheckSpelling

If IsObjectValid(FmFld) Then
...
Else
'If the user editted the text before the form field
intSubStringLoc = InStr(MyRange.Text, OldText)
If intSubStringLoc = 0 Then
oDoc.Undo
Exit Sub
End If

I could probably pop up a message informing the user that they
shouldn't be fiddling with the protected text... any thoughts?
 
D

David

I just run spell check until it finishes my document and when it starts
checking the forwarded document just cancell spell check and it will ask you
if you want to send it any way. Then just hit yes and there it goes without
checking all of the original mumble jumble.
 

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