Run time error 5887 accidentally posted in General

L

Lenny

I have created the following code in Module 1 of a Word Form w/fill-in fields
and have created a small toolbar with Protect and Unprotect buttons which
allows the user to unlock the form to access mail merge and then re-protect
the document per company requirements.

When it re-locks with password, only sections 1 and 3 are protected, 2 and 4
+ any additional sections that may be created are un-protected. When I first
ran the code it bombed, and based on the error message, realized the curson
was in a section that was protected in the code. When I placed the cursor in
section 4 (always unprotected), it worked a couple of times then started
giving me the error message 5887 and in the vb editor, highlights this line.
ActiveDocument.Sections(1).ProtectedForForms = True

What is wrong with the code and why did it work, then not?

Regards, Lenny

Sub ReprotectDocument()
'
' Reprotect Unlocked Document
' Macro recorded 6/29/2004

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:="my password"
If ActiveDocument.Sections.Count = 4 Then _
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = True
End If

End Sub
 
A

alborg

Hi Lenny:

You have to kill the error by placing an "On Error Resume Next" statement at
the beginning of your code. That error it seems occurs if the document has
less than 5 sections.

Also, Jay Freedman (MS Word MVP) gave a cleaner way to do things that you
might try-

Dim sec As Section
For Each sec in ActiveDocument.Sections
sec.ProtectedForForms = True
Next sec
ActiveDocument.Protect Password:="mypassword", _
NoReset:=False, Type:=wdAllowOnlyFormFields

URL:
http://www.wiredbox.net/Forum/Thread280904_Password_protect_all_sections_in_a_document.aspx

Hope this helps!

Cheers,
Al
 
L

Lenny

Alborg: my thanks for taking the time to respond and help... any idea why it
worked several times in a row, then didn't. At first it seemed that the
cursor had to be in an unprotected section of the document when the button
was clicked. But after a couple of 'protect', 'reprotect' clicks the error
message popped up. Just curious
 
A

alborg

Don't know- that is typical of Word VBA behavior, though. Occasionally this
has to do with timing issues.

Cheers,
Al
 

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