Help with MVP spellcheck protected form code

R

Roxy

I am trying to use the code from
http://word.mvps.org/faqs/macrosvba/SpellcheckProtectDoc.htm
and I got an error that the password was incorrect so I tried to follow the
directions from the website but I am still unsure where to insert
oDoc.Unprotect Password:="colleen"
And:
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:="colleen"
in to the code in the right places to get it to work. Help anybody? Thanks!
~Roxy
 
J

Jay Freedman

I am trying to use the code from
http://word.mvps.org/faqs/macrosvba/SpellcheckProtectDoc.htm
and I got an error that the password was incorrect so I tried to follow the
directions from the website but I am still unsure where to insert
oDoc.Unprotect Password:="colleen"
And:
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:="colleen"
in to the code in the right places to get it to work. Help anybody? Thanks!
~Roxy

In the code, find these lines:

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

and replace the oDoc.Unprotect line with

oDoc.Unprotect Password:="colleen"


Then find the lines

'Re-protect the document
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

and replace the second of those lines with

oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:="colleen"
 
R

Roxy

That work perfectly, thank you!
Now I have the macro set to run "on exit" on certain form fields, as I have
a 40 page document and don't want the spell checker to run thru the whole
thing, just bits at a time. But when I have it set on run "on exit" it
doesn't stop after just that particular form field it keeps on going. Is
there any way to have the code make it stop so you can continue to tab to the
next form field with out the spell checker running thru the entire document?
Thanks again for all your patience and continued help!

~Roxy
 
J

Jay Freedman

The macro from the article is built around the idea of checking all the fields
in the document in one pass. It would be quite difficult to make little changes
here and there to make it check only one field; it would actually be much easier
to write a completely separate macro to do that. Unfortunately I don't have the
time to do that now.
 
R

Roxy

I had been using the following code to try to just check individual form
fields, but ran into trouble cause I could figure out how to change the code
to do a range so it runs "on exit" and stops in just that form field instead
of what it is currently now doing and checking the field and then continuing
on and checking the rest of the fields in the document:

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

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:="colleen"

End If

'check formfield for spelling
For i = 1 To ActiveDocument.FormFields.Count
ActiveDocument.FormFields(i).Select
#If VBA6 Then
Selection.NoProofing = False
#End If
Selection.LanguageID = wdEnglishUS
Selection.Range.CheckSpelling
Next

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="colleen"
End If
End Sub
 
J

Jay Freedman

The code you have checks every field in the body of the document because of the
lines
For i = 1 To ActiveDocument.FormFields.Count
ActiveDocument.FormFields(i).Select

To spell-check only the field that's being exited, remove the For statement and
its matching Next statement, and change the second line above to this:

Selection.Bookmarks(1).Select

At the time the exit macro fires, the Selection is still in the formfield. The
Bookmarks(1) expression refers to the bookmark that is part of the formfield, so
selecting it also selects the contents of the formfield. That's all that will be
checked.

I can't guarantee that you won't run into trouble with this macro, because it
doesn't do anything to verify that its assumptions are met or to catch errors. A
lot of the macro on the MVP site consists of that kind of defensive code.
 
B

Bruce Kovacs

Is there an easy way to have the code present the prompt for the user to
enter the password instead of having to hard type it in the code?
 
G

Gordon Bentley-Mix

I'm sure you could use an InputBox for it - something like:

myPassword = InputBox("Enter Password","PASSWORD")
If myPassword <> "colleen" Then
MsgBox "That ain't it!"
Else
oDoc.Unprotect Password:=myPassword
End If

Or even:

On Error GoTo OOPS
oDoc.Unprotect Password:=InputBox("Enter Password","PASSWORD")
....
OOPS:
MsgBox "That ain't it!"

And depending on how you tricky you want to be, you could use a similar
approach for re-protecting the form:

oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:=InputBox("Enter Password","PASSWORD")

Or assuming you declare myPassword as a module-level variable:

oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:=myPassword

But if the users know the password, what's the point of protecting the form?
Aren't you worried that they'll just unprotect it and have a field day?
Kinda defeats the purpose of creating a password-protected form in the first
place IMO...
 
J

Jay Freedman

Also, if you're worried about some unauthorized person opening the VBA
editor and discovering the password in the code, you can apply a
password to the VBA project so it's unviewable unless you know that
password. Youll find that in the Tools | <project name> Properties
dialog, on the Protection tab.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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