Locking Your Solution’s VBA Project
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
You can prevent users from viewing code by locking each VBA project associated with your solution. To lock a project, you define a password that must be entered before a user can view the project in the Visual Basic Editor. You can lock the VBA project in Word, Excel, Access, PowerPoint, Outlook, and FrontPage. This section covers the procedure for locking a VBA project for all of these applications. For more information about security in Access, see Chapter 18, "Securing Access Databases."
You can lock the VBA project contained within a document, template, or database. For Outlook, you can lock the VBA project for the local session of Outlook, which is stored in the VbaProject.OTM file. Similarly, for FrontPage, you can lock the VBA project for the local session of FrontPage, which is stored in the Microsoft FrontPage.fpm file. You can lock only an entire VBA project, not individual components within the project. This means that when you lock a VBA project, you are controlling access to all standard modules (including the default ThisDocument, ThisWorkbook, and ThisOutlookSession modules for Word, Excel, and Outlook, respectively), all class modules, and all UserForms contained by the project.
Important VBA project passwords are case-sensitive. To view a locked VBA project, you must use the exact case you used when setting the password. If you lose or forget the password, there is no way to view the locked VBA project. When password-protecting a VBA project, be sure to write down the password and keep it in a physically secured location.
To lock a VBA project for viewing
Open the document, template, or database that contains the VBA project you want to protect. For Outlook or FrontPage, start Outlook or FrontPage on the computer that contains the VBA project you want to protect.
Open the Visual Basic Editor.
In the Project Explorer, right-click the project you want to protect, and then click ProjectName Properties on the shortcut menu.
- On the Protection tab, select the Lock project for viewing check box, enter and confirm the password, and then click OK.
The next time you open the document, the password you set will be required to view code in the project.
Important You can set a password without selecting the Lock project for viewing check box, but doing so only controls access to the Project Properties dialog box itself. If you set a password without selecting the Lock project for viewing check box, users will be able to view and modify your code.
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.
To determine if a VBA project is locked, set a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 object library, and then inspect the Protection property of the VBA project. The following example shows how to check the Protection property of the VBA project in a Word document.
Function IsWordProjectLocked(strDocPath As String) As Boolean Dim vbpProj As VBIDE.VBProject Dim docProj As Word.Document ' Open document. Set docProj = Documents.Open(strDocPath) ' Set reference to document's VBA project. Set vbpProj = docProj.VBProject ' Check Protection property of VBA project. If vbpProj.Protection = vbext_pp_locked Then IsWordProjectLocked = True Else IsWordProjectLocked = False End If End Sub
The IsWordProjectLocked procedure is available in the IsProjectLocked module in CheckProject.dot in the ODETools\V9\Samples\OPG\Samples\CH17 subfolder on the Office 2000 Developer CD-ROM.