Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Protecting a Word Document from Excel with VBA code

Status
Not open for further replies.

TDugan

Technical User
Oct 12, 2012
18
0
0
US
I have an excel VBA project and as part of that I launch a word doc and fill in information that was input on a userform in the excel doc(to prevent double-entry of data). The word doc has to be protected so that users can't alter the info.

I have the word doc protected to start with. Then at the start of that section of VBA code, I unprotect the document.
wdDoc.Unprotect Password:="XXXXXXXX"

Then once I populate all the info with my code, I need to re-protect the word doc from any changes.

I recorded a macro to get the code.
wdDoc.Protect Password:="XXXXXXXX", NoReset:=False, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False

It all works up until the re-protecting of the document. The document isn't protected. I can make changes. I don't get any error messages though.

I have tried different options on the Type argument. I have also tried setting the EnforceStyleLock argument = True instead of False.

Nothing seems to work. Any suggestions?

Thanks for any help you can provide.
 
Do you have "On Erroe Resume Next" somewhere earlier? Do you have reference to MS Word x.x object library?

combo
 
Might I suggest that a better way to do this is to leave the document protected throughout and, instead of using formfields, use Document variables (which you populate with your code) and DOCVARIABLE fields in the document to output their content.

In any event, without seeing the larger context of what's going on between your unprotect/protect lines, it's impossible to say what the issue is. Furthermore, once you do get it working, having 'NoReset:=False' is likely to give you a nasty surprise!

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks for your replies on this. I don't have an "On Error Resume Next" anywhere. I have the following code at the beginning.
Public Sub OpenWord()
Dim wdApp As Object
Dim wdDoc As Object
Set wdApp = CreateObject("Word.application")
wdApp.Visible = True

I'm going to pursue a different route and convert the word doc to PDF instead of re-protecting at the end.

Thanks again for the input received.
 
If you have not set a reference to Word, you cannot use constants like wdAllowOnlyFormFields. You'll have to use the Object Browser in Word to find the actual numeric value.

Or you could Tools > References and drill down to Microsoft Word Object Library.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
So this code should not work with late binding, it should of give you an error:
[tt]
Dim wdApp As Object
Dim wdDoc As Object
Set wdApp = CreateObject("Word.application")
wdApp.Visible = True

Set wdDoc = ...

wdDoc.Protect Password:="XXXXXXXX", NoReset:=False, Type:= _[red]
wdAllowOnlyFormFields[/red], UseIRM:=False, EnforceStyleLock:=False
[/tt]

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
it should of give you an error
Unless there is no Option Explicit ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Try:
wdDoc.Protect "XXXXXXXX", True, 2, False, False

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top