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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Protect-Unprotect VBAProject Programatically

Status
Not open for further replies.

Scottflyer

IS-IT--Management
Mar 15, 2003
4
US
Hi:

I want to protect and unprotect -- with password from a VBA subroutine or function. I'm currently using the following code, but it only works if the VBA Project is open or unprotected, which doesn't serve my purpose:

Set VBP = ActiveWorkbook.VBProject
If ActiveWorkbook.VBProject.Protection = vbext_pp_locked Then
SendKeys "~" & MdP & "~", True
End If
With VBP.VBComponents
.Remove VBP.VBComponents("Module3")
.Remove VBP.VBComponents("Module5")
End With

I'd prefer to unprotect the project with code, do several operations on modules, like .Export, .Import, .Remove to do a software update for example, etc. and then protect the project at the end of the code. I don't know the method(s) for VBA Project protection and unprotection and the way the password is passed to them [Excel in Office XP]. If anyone can help. Thanks very much!!

Scottflyer
 
Scottflyer,

If you want to access a workbook with out having it active try using:

WorkBooks("myWorkBook")

Instead of:

ActiveWorkbook

To protect the workbook, use the following:

Workbooks("myWorkBook").Protect "Password", True, True

The first password is case sensitive, so be carefull. The second parameter specifies whether to protect the worksheet order, and the third parameter specifies whether to protect the workbook window.

To Unprotect the workbook, use the following:

Workbooks("myWorkBook").Unprotect "Password"

Hope this helps,

Schizo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top