Using VBA to remove worksheet protections

B

Brian Beck

I have a form that end users have been using to submit reports. There are
numerous form fields within the document and the data within those fields is
what I'm interested in. It's relatively easy for me create a macro that
simply sets the document to save form data only and then save the data. I
figure I can just load each form up, run the macro, close the form...and
repeat. Except for one little problem. When I created these forms
originally, I had password protected them so that the user could only enter
information into the form fields. I'd like to also automate the process of
un-protecting the document, but I can't seem to figure out how to do that.

I know that I can put some code in an AutoOpen macro that I can use just for
the purpose of messing with these forms, but in using the syntax below, I
keep getting an error [Runtime error "13" Type mismatch]:

Sub AutoOpen()
With Options
.LocalNetworkFile = False
.AllowFastSave = False
.BackgroundSave = True
.CreateBackup = False
.SavePropertiesPrompt = False
.SaveInterval = 10
.SaveNormalPrompt = False
.DisableFeaturesbyDefault = False
End With
With ActiveDocument
.ReadOnlyRecommended = False
.EmbedTrueTypeFonts = False
.SaveFormsData = True
.SaveSubsetFonts = False
.DoNotEmbedSystemFonts = True
.Password = ""
.WritePassword = ""
.DisableFeatures = False
.EmbedSmartTags = True
.SmartTagsAsXMLProps = False
.EmbedLinguisticData = True
.Protect "wdNoProtection,,PASSWORD,,"
End With
End Sub

Obviously, PASSWORD would be replaced with the actual text of the password
for the form.

Any ideas are greatly appreciated.

-Brian
 
T

Tony Jollans

The reason for the type mismatch is that you have passed a string as the
first parameter when the Protect method expects a number.

However, if you correct that you will get a different error telling you that
you can't protect an already protected document.


To unprotect a document, use ActiveDocument.Unprotect.
 

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