Protect/Unprotect VBAProject Properties

Robb

Board Regular
Joined
Feb 17, 2002
Messages
145
My Excel program is protected, but I have a problem. I have to Unprotect the project when I do a certain task and then I would like to re-protect it from unwanted eyes/fingers. Can I do this in code? I tried this and it doesn't seem to work...

' We have to unprotect the project
VBAProject.ThisWorkbook.Unprotect password:="test123"

' do some stuff here

' Now re-protect the project
VBAProject.ThisWorkbook.Protect password:="test123"
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Well, I've been looking for some property/method/event, or something, but I can't seem to find a way to do that... which would seem logical to me, to avoid creation of crack subs directly in VBA (Yes, yes, I know there are many programs that can do that, but no of them is built in VBA, as far as I know...)

One, not very good way and don't know how reliable, would be using SendKeys.
 
Upvote 0
WHY? Good question...

I am creating and removing userforms on the fly. You can't create a userform if the Project is protected! I've used SendKeys before where would there be an example?
 
Upvote 0
Here is a method I used some time ago
that protects and unprotects with a paasword.

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 = "ivan"
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

SendKeys BreakIt & Password & "{tab}" & Password & "~" & "%{F11}~", True
'SendKeys "enter", True
WB.Activate
SendKeys "%{F11}", True
Application.ScreenUpdating = True
End Sub




Ivan
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top