Chris,
I just did a quick check of the VBproject object model and don't find anything that indicates it is possible to protect or unprotect a VBproject from within VBA. VBA can only test (read-only) whether a VBproject is protected or not. If would have very interesting implications if a VBproject's protection could be changed from VBA, because it would mean that a VBproject could set or remove its own protection.
One thing you might therefore try as a last resort is to remove and reset the protection using SendKeys to actually send the necessary keystrokes to the VBE user interface.
Damon
Chris,
Trry something like this;
Option Explicit
Const BreakIt As String = "%{F11}%TE+{TAB}{RIGHT}%V{+}{TAB}"
Sub Change_VBA_PW()
Dim WB As Workbook
Dim Password As String
Set WB = ActiveWorkbook
Password = "test"
Call SetVBProjectPassword(WB, Password)
End Sub
Sub SetVBProjectPassword(WB As Workbook, ByVal Password As String)
'Needs reference to Visual Basic for Applications Extensibility Library
Dim VBP As VBProject
Dim OpenWin As VBIDE.Window
Dim i As Integer
Set VBP = WB.VBProject
Application.ScreenUpdating = False
' close any code windows to ensure we are in the right project
For Each OpenWin In VBP.VBE.Windows
If InStr(OpenWin.Caption, "(") > 0 Then OpenWin.Close
Next OpenWin
WB.Activate
'Application.OnKey "%{F11}"
SendKeys BreakIt & Password & "{tab}" & Password & "~" & "%{F11}~", True
'SendKeys "enter", True
Application.ScreenUpdating = True
WB.Activate
SendKeys "%{F11}", True
End Sub
Ivan