How do I lock a Word Project using VBA

Posted on 2006-04-08
Last Modified: 2008-01-09
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
Question by:TimLitle
    LVL 92

    Expert Comment

    by:Patrick Matthews
    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.


    LVL 76

    Expert Comment

    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.

    Author Comment

    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 . . .?

    Author Comment

    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.
    LVL 76

    Accepted Solution

    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.

    Author Comment

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
    Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
    This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
    The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now