Programmatically Open Locked VBAProject

M

Michael PE

I'd like to open a password protected VBAProject via code, copy out specific
modules, then close the project (add-in).

I have an add-in that I share with multiple functions that are used by
users. I have the add-in VBAProject password protected. This is enough to
keep the nosey out. I know it is NOT secure.

Now, I would like the user to copy modules (that I have specified) out of
the add-in vbaproject to their workbook. I have the code to copy the module
but I cannot do it if the add-in project is locked.

How do I open the add-in VBAProject, pass the password to it, then close it
again after it the copy module function is complete?

One Response:
What you are asking is considered hacking and you will not get any help here
as it is against board policy. You may have to find a different way to make
your code available to the user.


My Reply:
Okay. If it considered hacking. How do I go about sharing portions of my
code but not all of it that is contained inside my add-in without allowing
all of my code to be exposed to nosey users?

Is it still considered hacking if I know what the password is and pass it
via code to open it? For example, while researching on here, I found posts of
how to pass the password to a protected sheet to unprotect it. Is that not
the same thing?

Why is it considered hacking if I wrote the code that is protected and
placed the password protection?

If there is a better way to copy specific modules from my password-protected
(where I created and know the password) add-in to a users workbook, please
let me know.
 
X

XP

Hi Michael,

Personally, I would not consider this to be hacking; but what do I know?

Unfortunately, I think the only way to do this is to use send keys (but I
could be wrong). The following function works splendidly for me. You need to
be sure that "Trust access to visual basic project" is checked in the source
file you are trying to access, and also, set a reference to "MICROSOFT VISUAL
BASIC FOR APPLICATIONS EXTENSIBILITY" for this to work.

Note that this function below unlocks the project of the current file, I
haven't tried it to unlock a different file, but it seems like you should be
able to manipulate the function to unlock the VBE in a specified file (which
may need to be activated first).

Call the function like so:

Call UnlockVBAProject(MyPassword)

Here is the function placed in a standard code module:

Private Function UnLockVBAProject(argPWD As String)
'unlock VBA project:
Dim VBP As VBProject
Set VBP = ThisWorkbook.VBProject
If VBP.Protection <> vbext_pp_locked Then Exit Function
Application.ScreenUpdating = True
DoEvents
SendKeys "%{F11}%TE" & argPWD & "~~%{F11}", True
DoEvents
End Function

This may help at least to get you started.
 
X

XP

Just another thought,

Place the unlocking function in a module in the file you want to unlock,
then call it from your program file or your controlling file and you're done.

HTH
 
J

Jon Peltier

The techniques are hacking, even if that's not YOUR intention, because they
would help anyone hack into a protected VB project.

If you have modules that you want to share, distribute them in another
unprotected project or as exported modules. This ensures that there are no
problems with SendKeys or with your modules' security (such as it is).

- Jon
 
D

Dave Peterson

Just to add to Jon's idea...

Maybe you could create template files that contain only the procedures/functions
that you need to share. The template project's could still be protected.
 
J

Jon Peltier

Dave -

Now that you mention it, that's how I handle code in one of my big projects.

- Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top