Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1223
  • Last Modified:

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
0
TimLitle
Asked:
TimLitle
  • 3
  • 2
1 Solution
 
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.

Regards,

Patrick
0
 
GrahamSkanRetiredCommented:
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
   Loop
End Sub

Though I think you would need a counter to pull you out if it's never going to work for this session.
0
 
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:

Documents.Add
strNewDoc = ActiveDocument.Name
Windows(2).Activate
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 . . .?
0
[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
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.
0
 
GrahamSkanRetiredCommented:
It is supposed to be impossible by design. This Microsoft article:
http://www.microsoft.com/technet/prodtechnol/office/office2000/proddocs/opg/part4/ch17.mspx
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.
0
 
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 http://www.microsoft.com/technet/prodtechnol/office/office2000/proddocs/opg/part4/ch17.mspx article was very helpful.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now