On exit command

R

Roxy

I want to know if you can have more than one on exit command for a form field
in a protected document? And if so how do you go about doing that. Any help
would be greatly appreciated.
Thanks in advance!
~Roxy in Alaska
 
J

Jay Freedman

I want to know if you can have more than one on exit command for a form field
in a protected document? And if so how do you go about doing that. Any help
would be greatly appreciated.
Thanks in advance!
~Roxy in Alaska
No, there can be only one On Exit macro per field. If you need the macro to have
different behavior based on the field's content or some other criterion, you'll
have to program that into the macro itself.
 
R

Roxy

Thank you for your speedy reply.
Is there any way you would be willing to help me with my code that I have
currently written? I could send you the document and you could look at it to
see the trouble I am having with spellchecking only certain form fields and
not the entire document. I need to figure out what language to insert after
the end of the spellcheck code to have it STOP THERE and then go on to the
next bookmark let the user enter data and so on. I have been practicing on a
1 page doc before I mess up my 40 pager that I am really writing this for.
Any help would be great I have been exhausting all my resources on the web
for the last 4 weeks on this issue :(
Thanks a bunch from Alaska
 
J

Jay Freedman

I'm at a conference away from home and posting only when I get a few minutes
here and there. If you don't mind waiting until next weekend for an answer, you
can send the document (the email address in my profile is live) and I'll see
what I can do. Please describe in as much detail as possible exactly what you
want the macro to do.
 
J

Jean-Guy Marcil

Roxy said:
Thank you for your speedy reply.
Is there any way you would be willing to help me with my code that I have
currently written? I could send you the document and you could look at it to
see the trouble I am having with spellchecking only certain form fields and
not the entire document. I need to figure out what language to insert after
the end of the spellcheck code to have it STOP THERE and then go on to the
next bookmark let the user enter data and so on. I have been practicing on a
1 page doc before I mess up my 40 pager that I am really writing this for.
Any help would be great I have been exhausting all my resources on the web
for the last 4 weeks on this issue :(

The code you have been using from the mvps web site was designed to spell
check a whole document when a *user* decides it is time to do the spell check
(by clicking on a button or some other similar input method). As a user I do
not know if I would like to have the spell check kick in after every form
field in a 40-page document... I might prefer to check the spelling in one go
at the end...

That code was not designed to be used "OnExit" from a formfield in order to
check only that formfield...
If that is what you want to do, try the following:


Dim rngField As Range
Dim i As Long

With ActiveDocument
.Unprotect
Set rngField = Selection.Bookmarks(1).Range.FormFields(1).Range
i = .Range(1, rngField.End).FormFields.Count

.Range.NoProofing = True
'.SpellingChecked = True

With rngField
.NoProofing = False
.CheckSpelling
End With

.Range.NoProofing = False

.Protect wdAllowOnlyFormFields, True
If i < .FormFields.Count Then
.FormFields(i + 1).Range.Select
End If
End With


I have to mess about with
.Range.NoProofing = True
because I did not find a way to stop the spell check at the end of the
current formfield range. It seems it wants to go on to the end of the
document...
There might be a way to control the spell checker so that it behaves and
spell checks only the targeted range. On the other hand, it would not be the
first time that a workaround is necessary because of some flaw in the Word
Object model. I decided to set the whole document to "No Proofing." You
could, instead, set the whole document to a state indicating that spell
checking has been performed:
'.SpellingChecked = True
I commented out that line in the code above. You could use it instead. I am
not sure what the pros/cons would be with using NoProofing vs. using
SpellingChecked.

Good luck!
 
R

Roxy

Thank you so much Jean-Guy for taking the time to do up that code for me. I
tried it but my form is protected with a password, so I got an error message,
"Runtime Error '5485' the password is incorrect." I am very new to all this
macro stuff so I am not positive what the language would be to unprotect with
a password and then reprotect with a password so I tried my best. But then I
got another error message, "Compile Error: End With without With." Now I am
completely lost, please help! I am so, so excited to try out your code!!!

Sub Spellcheck()

Dim rngField As Range
Dim i As Long

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

Set rngField = Selection.Bookmarks(1).Range.FormFields(1).Range
i = .Range(1, rngField.End).FormFields.Count

.Range.NoProofing = True
'.SpellingChecked = True

With rngField
.NoProofing = False
.CheckSpelling
End With

.Range.NoProofing = False

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="colleen"
If i < .FormFields.Count Then
.FormFields(i + 1).Range.Select
End If
End With
End Sub
 
J

Jean-Guy Marcil

Roxy said:
Thank you so much Jean-Guy for taking the time to do up that code for me. I
tried it but my form is protected with a password, so I got an error message,
"Runtime Error '5485' the password is incorrect." I am very new to all this
macro stuff so I am not positive what the language would be to unprotect with
a password and then reprotect with a password so I tried my best. But then I
got another error message, "Compile Error: End With without With." Now I am
completely lost, please help! I am so, so excited to try out your code!!!

It seems you still want to use the password "colleen". Make sure you
*manually* protect the from giving it that password *before* trying the macro.

Then use this version, which is identical, except it has the password added
to the code:

Dim rngField As Range
Dim i As Long

With ActiveDocument
.Unprotect "colleen"
Set rngField = Selection.Bookmarks(1).Range.FormFields(1).Range
i = .Range(1, rngField.End).FormFields.Count

.Range.NoProofing = True
'.SpellingChecked = True

With rngField
.NoProofing = False
.CheckSpelling
End With

.Range.NoProofing = False

.Protect wdAllowOnlyFormFields, True, "colleen"
If i < .FormFields.Count Then
.FormFields(i + 1).Range.Select
End If
End With

If you manually protect the document with a password, you do not need to
check the type of protection and whether it is protected or not; you know it
is protected and you know the type of protection. The only way a user could
make the code fail (meaning the type of protection has changed, or it is not
protected anymore...) is by cracking the passowrd and then mucking about in
the document. There is not much you can do against such users.

Normally, you do that type of checking with documents that are not password
protected. In those cases, even well-intentioned users could change something
without really meaning to...
 

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