Spellchecking a Protected Document

  • Thread starter NZ VBA Developer
  • Start date
N

NZ VBA Developer

This seems to be a popular topic lately, so I thought I'd chime in as well.

While the article at
http://word.mvps.org/faqs/macrosvba/SpellcheckProtectDoc.htm is great if you
want to check the spelling of the information input into formfields, my
problem is a bit different. I have (Word 2003) templates that produce
forms-protected documents, but the documents themselves don't contain any
formfields. I use forms protection only because it allows me to protect only
certain sections of the document, such as the header and footer, while
leaving other sections, such as the main body of the document unprotected.

The spellchecker checks the spelling in the unprotected sections OK, and I
don't really mind if it doesn't check the spelling in the protected sections
because 1) I know that I haven't made any spelling errors in the
'boilerplate' in these sections; and 2) if the user has made a spelling error
in the 'variable' information, they can just rerun the code in template to
correct the errors. However, there is a deficiency in the native spellchecker
that I've been asked to develop a workaround for.

In an unprotected document, if none of the choices in the 'Suggestions' list
are viable, you can just type in the 'Not in Dictionary' box to make the
change. However, this doesn't work in a protected document; the text in the
'Not in Dictionary' box is treated like it's in a protected section. I could
always write code that unprotects the document before displaying the
spellchecker, but there are a couple of drawbacks:

First, this will allow the users to spellcheck the protected sections as
well, which is not really a major problem except that if the user corrects an
error that was introduced through the input of the 'variable' information
described above, this correction won't get replicated back into the data from
the UserForm. Thus if the user reruns the template, the correction won't be
there and the error could be reintroduced without the user realising it.
However, I can live with this.

The real problem is that the spellchecker dialog isn't modal to Word.
Therefore, if I unprotect the document, the user now has full access to the
whole document, _including_ the protected sections. And believe me, the users
will take advantage of this whenever possible; I learned this when I told the
users I was going to fix a bug in my code that left the document unprotected
and got a... umm... less than favourable response. ;-D

Does anyone know a way to enable the editing capability in the 'Not in
Dictionary' box without unprotecting the document? Or will my users just have
to live with this limitation? (The second choice is acceptable as I'm quite
happy to tell the users 'No' - as long as I can justify the response.)
--
Cheers!
The Kiwi Koder

Please note: Uninvited email contact will be marked as SPAM and ignored -
unless you want to hire me.
 
G

Graham Mayor

How about limiting the spell check macro to the range that you actually want
spell checked? eg for section 2

Sub SpellCheckForm()
Dim i As Integer
Dim bProtected As Boolean

With ActiveDocument
If .ProtectionType <> wdNoProtection Then
bProtected = True
.Unprotect Password:=""
End If

.Sections(2).Range.Select
With Selection
#If VBA6 Then
.NoProofing = False
#End If
.LanguageID = wdEnglishUK
.Range.CheckSpelling
End With

If bProtected = True Then
.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
N

NZ VBA Developer

Graham,

Thanks for the suggestion. I'll have to test it to see if it meets my needs;
however, I see a couple of problems straight away:

First, it's not the protected sections that I want to spellcheck; it's the
_unprotected_ ones, and the users want to be able to use Word's native
'Spelling and Grammar' dialog to do this. Does
'Selection.Range.Checkspelling' invoke this dialog or just do the 'red
squiggly underline' thing?

Second, the sections that are unprotected vary from document to document, so
it's not always possible to use hardcoding to specify which sections to check
- altho it's a bit of a moot point in light of the above.

And finally, if this code does invoke the 'Spelling and Grammar' dialog,
this dialog is not modal to Word. Consequently, 'ActiveDocument.Unprotect'
will cause me no end of grief, as the users will just run the spellchecking
macro and then hop in behind the dialog and start mucking about with the
stuff that's meant to be protected. (I could write great code if it weren't
for the bloody users!)

Anyway, thanks for the suggestion. I think I'll just tell the client that
they can't have full functionality from the 'Spelling and Grammar' dialog due
to a limitation in Word. I've had to do that often enough with other things
that they're getting used to it now. ;-D
 
G

Graham Mayor

The code does invoke the spelling dialog and checks the unprotected section
which is what you asked for. I can't think of a way off hand to establish
which of a random variety of sections were previously protected and limit
the function to only those sections. However surely the protected and
unprotected sections are fixed in the form template? If not and you are
going to allow such free expression in the document then I have to wonder
whether a form is the ideal way of doing the job?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jean-Guy Marcil

NZ VBA Developer said:
Graham,

Thanks for the suggestion. I'll have to test it to see if it meets my needs;
however, I see a couple of problems straight away:

First, it's not the protected sections that I want to spellcheck; it's the
_unprotected_ ones, and the users want to be able to use Word's native
'Spelling and Grammar' dialog to do this. Does
'Selection.Range.Checkspelling' invoke this dialog or just do the 'red
squiggly underline' thing?

Second, the sections that are unprotected vary from document to document, so
it's not always possible to use hardcoding to specify which sections to check
- altho it's a bit of a moot point in light of the above.

And finally, if this code does invoke the 'Spelling and Grammar' dialog,
this dialog is not modal to Word. Consequently, 'ActiveDocument.Unprotect'
will cause me no end of grief, as the users will just run the spellchecking
macro and then hop in behind the dialog and start mucking about with the
stuff that's meant to be protected. (I could write great code if it weren't
for the bloody users!)

Anyway, thanks for the suggestion. I think I'll just tell the client that
they can't have full functionality from the 'Spelling and Grammar' dialog due
to a limitation in Word. I've had to do that often enough with other things
that they're getting used to it now. ;-D

In this case, have you looked up the
Sections(n).ProtectedForForms
property?

You can use that to establish which sections are protected, for example:

Dim i As Long
Dim strTest As String

With ActiveDocument.Sections
For i = 1 To .Count
If .Item(i).ProtectedForForms Then
strTest = ""
Else
strTest = "not "
End If
MsgBox "Section " & i & " is " & strTest & "protected.", _
vbInformation, "Results"
Next
End With

Now, whether or not you can use that to spell check target sections or not
is another matter... Spell checking ranges is hard to handle an sometimes
unpredictable.

Also, you do not have to worry about modality... The Code-invoked Spelling
dialog is modal... user cannot get behind it... which is different from when
you invoke it through the UI...

Good luck!
 
N

NZ VBA Developer

Hmm... hmm... hmm...

~scratching head~

Interesting possibilities abound! But I think I've just about exhausted the
budget on this project. Time to put this one in the too-hard bin and tell the
client to live with the limitation - at least until the next release.

Altho if the code-invoked Spelling dialog is modal, it might be enough just
to unlock the document and call it. However, it still doesn't get around the
problem with spellchecking the protected sections, which opens another can of
worms entirely... Best just left, methinks.
 

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