How do I lock a Word Project using VBA

When I work on a project, I do it in a development environment then “Install” it into a production environment.  The “Installation” process is controlled by VBA and it makes sure various parameters are set properly.  There are two additional things I wish to do during this process which I have separated into two separate questions.  Question 1 is:

How do I use VBA to do the equivalent of  “Tools | Project Properties | Protection” then check “Lock project for viewing” then enter and confirm a password?  The following works erratically and is a kludge.  Is there a better way?  Is there some way to make it work properly and consistently?

Sub LockProject()
    If ActiveDocument.VBProject.Protection = vbext_pp_locked Then Exit Sub                  ' Exit if already locked
    SendKeys "+{Tab}{Right}%v{+}{Tab}Password{Tab}Password{Enter}"                    ' Keystrokes to Lock and enter password "Password"
    Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute    ' Put keystrokes in the "Tools | Project Properties | Protection" menu
End Sub
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Hi TimLitle,

You're right about it being a kludge, but I do not think there is any other way.  Which makes sense: what little
security value locking the VBProject already has would be totally negated if the Word or VBIDE object models
allowed a direct way to toggle this one.


I agree with Patrick to the extent that SendKeys is the only way. The Protection property is read-only.
I don't think that it would directly break the security, though. It would still need a password..

It might help to retry the Sendkeys:

Sub LockProject()
    Do while ActiveDocument.VBProject.Protection = vbext_pp_locked 'Then Exit Sub                  ' Exit if already locked
        SendKeys "+{Tab}{Right}%v{+}{Tab}Password{Tab}Password{Enter}"                    ' Keystrokes to Lock and enter password "Password"
        Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute    ' Put keystrokes in the "Tools | Project Properties | Protection" menu
End Sub

Though I think you would need a counter to pull you out if it's never going to work for this session.
TimLitleAuthor Commented:
I have made progress on this and can get it to work reliabily on a document associated with my code.  It turns out that what I really want to do is to create a new blank document and set THAT document's protection properties to locked.  I use VBA from the original document to add code modules and close the new document.  When I open new document up again, if it works as hoped, the code would be protected.

VBA help says that the SendKeys statement "Sends one or more keystrokes to the active window as if typed at the keyboard."  Therefore, I tried the following but it doesn't work:

strNewDoc = ActiveDocument.Name
Debug.Print vbCrLf & " Active Window: " & Windows(2).Caption    ' To make sure that the right window is activated
SendKeys "{Home}{Home}{Home}{Home}^{PgUp}%v{+}%pPassword%cPassword~"
Documents(strNewDoc).VBProject.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute

Any ideas?

Also, where does one find a list of Control IDs as in "FindControl(ID:=2578 . . .?
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

TimLitleAuthor Commented:
I would still like an answer to this but alas, I have not received a way to solve my problem.

My workaround is to put up a message that reminds the user to set the password manually which is obviously less than idea.
It is supposed to be impossible by design. This Microsoft article:
actually contains this paragraph:

There is no way to programmatically specify a password for a locked VBA project. If you write code that attempts to work with components in a locked VBA project, the Visual Basic Editor will display a dialog box to prompt the user for the correct password. If the user specifies the correct password, your code will continue to run. If the user doesn't specify the correct password, a trappable error will be returned.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimLitleAuthor Commented:
I am liiking for a way to programmatically use a specified password to LOCK an UNLOCKED project.  the use of "Sendkeys" seems to be theonly way and that is unreliable at best.

For a variety of reasons, the article was very helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.