Code to unprotect VBAproject

BX

Board Regular
Joined
Jun 2, 2002
Messages
172
Hi
I have protected my project to hide the code from users. However, in one of the macros I use prior to sending the file, I delete extra sheets and user forms. Since the project is protect the code results in an error.
At the start of the code is there away to unprotect my project and then at the end reprotect the project.

something like: project ("name").unprotect "1234"
obviously not right, but what is the proper way to refer to a project and unprotect and re-protect it.

Thanks,
BX
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Not 100% here as my VBA isn't too strong but I don't think there is a way to unprotect your project in that way (though I may be proven wrong) - perhaps if you made your project into an addin would work though - someone here is bound to know though
 
Upvote 0
From J-Walk Excel 97/2000 Developer FAQ which I think applies to what you want to do also

How can I write a macro to change the password of my project?

You can't. The protection elements of a VBA project are not exposed in the object model. Most likely, this was done to make it more difficult for password cracking software
 
Upvote 0
Thanks for the reply.
I've also been doing some research and have started to come to the conclusion I can't unprotect my project in code even though I know the password.
However, would it be possible to do the opposite: protect the project in code?
Just by chance...I doubt you can also do this, but if you know please pass on your response.
Cheers,
BX
 
Upvote 0
i'm sure this was by design because if you could place a line of code at the start of a module to unprotect, and then a line of code at the end to re-protect, a user could run the macro, hit control break to manually interrupt/end the macro (after the project has been unprotected, but before it could reach the line of code that re-protects it) thereby gaining access to the code behind the module. wouldn't be very secure

kevin
 
Upvote 0
Well, actually you can unprotect a VBA project with code, assuming you know the password (and still possible if you don't with some password cracking software out there). However, since you are the workbook author and you know the password, the code below isn't morally illegal.

This uses the dreaded and hated SendKeys method to simulate the actions of going to the VBE, then to the Project Explorer window, then selecting the Project name, and unrpotecting it. I placed some notes in front of the significant code lines.

One other note on something that looks excessive is all the Tab lines, which is one trick to this code you will also need to know - - that being, where in the PE your project exists. The quantity of Tab lines is variable depending on your add-in and current project task situation.

This is run from a separate workbook, targeting whatever workbook you want to open and unprotect the VBE for. After that you can do whatever you want to do in the workbook code-wise, and when you close it with a line like
Workbooks("YourFileName.xls").Close True
Upon close, the VBE will reprotect itself as usual, meaning YourFileName.xls will be locked the next time it opens.

The code which worked on Excel 2002 (and should work on Excel 2K but not guaranteed for 97 or before) is:

Sub TestUnprotectProject()
Workbooks.Open "C:\Your\File\Path\YourFileName.xls"
With Application
'Go to the VBE
.SendKeys "%{F11}", True
'Activate Project Explorer window
.SendKeys "^r", True
'Tab your way to the workbook project
'Note - - when I tested this, it was in a separate workbook,
'following 6 add-in projects.
'"YourFileName" was the 8th project in my PE window,
'so 9 tab hits were required.
'You could also down-arrow using .SendKeys "{DOWN}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
.SendKeys "{TAB}", True
'Simulate the Enter key
.SendKeys "~", True
'Enter your password - - this example is "Password"
.SendKeys "Password"
'Enter again
.SendKeys "~", True
End With
End Sub
 
Upvote 0
good point Tom, I never even thought of sendkeys - but I still wouldn't put code like that into a workbook that I was going to distribute. otherwise what would be the point of protecting the project? unless you were going to change the "Password" part of the code to an inputbox - yes, you still have to enter a password when prompted, but it would still be easier than having to open the VBE.

kevin
 
Upvote 0
BX said:
Thanks for the reply.
I've also been doing some research and have started to come to the conclusion I can't unprotect my project in code even though I know the password.
However, would it be possible to do the opposite: protect the project in code?
Just by chance...I doubt you can also do this, but if you know please pass on your response.
Cheers,
BX

YES, you can, but in all instances you have to use the Gludgy Sendkeys
as Toms code has........Have a look here...there are a number of variations to doing this, and I have tried some with API's

http://216.92.17.166/board2/viewtopic.php?t=99&highlight=vbe
 
Upvote 0
Kevin - -

Before posting, I looked but didn't see where BX said it'd be distributed, and I agree with you, I wouldn't rely on it for distribution either, which was what I alluded to regarding the trick to knowing which position in the Project Explorer the workbook was. This code may have some value for users on a shared drive, for example.

Sort of a boring pre-holiday Friday on the left coast so I thought I'd provide an example of how the oft-maligned SendKey approach might have applicability to accomplish something obscure. And as Ivan shows, API offers more options.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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