PDA

View Full Version : Protection of the VBA code



jungix
08-30-2006, 11:36 AM
Hi,

I have a macro in an Excel worksheet to do a lot of stuff, and I would like to send the Excel file, but I don't want him to read the code. How can I send this Excel file but locking the VBA code behind, so that I would be the only one to be able to access it?

RichardSchollar
08-30-2006, 11:43 AM
Hi Jungix

In the VBE, click on your workbook in the Projects pane and go Tools>VBA Project Properties>Protection tab and check the "Lock Project for viewing" box and input a password. Click OK, save it down, then whenever anyone tries to open the VBA code module, they'll be asked to input the password.

Best regards

Richard

jungix
08-30-2006, 12:22 PM
Thank you very much this is exactly what I needed!

Zack Barresse
08-30-2006, 03:07 PM
Please be aware about Excel and passwords, it is very easy to break them. VBE passwords are a little trickier, but still very easy. Excel should not be thought of as a "secure" application, not at all.

jungix
08-31-2006, 05:51 AM
I wouldn't know how to do this. I think someone capable of doing this would be able to rewrite my code, and it would probably be quickier for him anyway ;)

Zack Barresse
08-31-2006, 07:39 AM
LOL! That's the way I figure it. Just didn't want to give any false impressions that this is in anyway a secure environment. :)

Ken Puls
08-31-2006, 08:34 AM
I think someone capable of doing this would be able to rewrite my code,

That is probably true.


and it would probably be quickier for him anyway ;)

Not that it makes a difference to the point overall, but it can be done in less than a minute with the right tools and knowledge.

Zack Barresse
08-31-2006, 08:52 AM
Ken, I've *seen* under 20 sec. ;)

Ken Puls
08-31-2006, 08:55 AM
Yes? Is that not under a minute? LOL!

I was just trying to illustrate that it would take less time than writing much of anything. ;)

MrRhodes2004
10-03-2006, 11:55 AM
The way that was suggested locks the entire VB Project. Is there a way to lock just one module of the project? I have some code that may need to be adjusted by others but there is some that I do not want others to view or change.

Can protection be placed on one module only?

Ken Puls
10-03-2006, 12:18 PM
Nope, not that I'm aware of.

What you could do, though, is release the code you want to hide in a separate, protected add-in or workbook. (We'll call it wbPrivate, for this.) You'd have to play with it a bit, but you could call a routine to load the wbPrivate workbook, leverage the code, then unload it. Keep the vbProject in wbPrivate protected, and it may accomplish what you're after.

HTH,

Zack Barresse
10-03-2006, 12:49 PM
Along Ken's idea, if you didn't want the entire workbook (wbPrivate) viewable, only certain modules, you could create a new workbook and import the modules from wbPrivate.

Ken Puls
10-03-2006, 01:14 PM
I think that there's some code samples in the KB for importing modules, and most likely removing them afterwards too. :)

matthewspatrick
10-03-2006, 07:20 PM
I think that there's some code samples in the KB for importing modules, and most likely removing them afterwards too. :)

But this all gets trickier in Excel 2002 and later. Excel 2002+ is delivered with 'trust access to vb project' turned off, so any scheme to import/remove modules will fail unless that gets turned back on. Telling your users to turn it on will only make them start asking questions (and I've heard that some orgs use security policies to keep it off permanently).

In the end, no scheme that relies on VBA and an Office app is truly secure. If you must protect the code, compile it as an exe/dll/xll.

Ken Puls
10-03-2006, 09:57 PM
True enough, Patrick. :yes