Import module

C

Charlotte E

I need my VBA to import a module depending on certain conditions.

Problem is that the project is 'hidden from viewing' with a code, and
apparrently this also blocks for modules to be imported!

Is there a way to temporary unlock the project, import the module, and then
protect the project again?

I'm not talking about cracking the project - the code is known...

....just need to temporary allow for a module to be imported by VBA code (the
importing code, I already know :)


Thanks in advance...
 
D

Dave Peterson

There's nothing built into excel/VBA that allows you to unprotect a protected
project.

You may be able to search google for techniques that use Sendkeys, but I
wouldn't use anything that is as unreliable as that.
 
I

isabelle

hi,

Sub TestUnprotectVBA()
'the password = "a"
Workbooks.Open Filename:="C:\temp\MyFile.xls"
Call UnprotectVBProject(ActiveWorkbook, "a")
End Sub

Sub UnprotectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object
Set vbProj = WB.VBProject
If vbProj.Protection <> 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = vbProj
SendKeys Password & "~~"
Application.VBE.CommandBars(1).FindControl(ID:=2578, _
recursive:=True).Execute
End Sub
 
G

GS

Charlotte E wrote :
I need my VBA to import a module depending on certain conditions.

Problem is that the project is 'hidden from viewing' with a code, and
apparrently this also blocks for modules to be imported!

Is there a way to temporary unlock the project, import the module, and then
protect the project again?

I'm not talking about cracking the project - the code is known...

...just need to temporary allow for a module to be imported by VBA code (the
importing code, I already know :)


Thanks in advance...

Is there some reason the module can't be included in the project and
only use its code under certain conditions?
 
C

Charlotte E

Is there some reason the module can't be included in the project and only
use its code under certain conditions?

It contains XL2007 code, which won't complie under XL2003 - so, I check for
Application.Version, and import the module if Version is > 11.

Works like a charm :)
Unless if project is hidden from viewing :-(

CE
 
C

Clif McIrvin

Charlotte E said:
It contains XL2007 code, which won't complie under XL2003 - so, I
check for Application.Version, and import the module if Version is >
11.

Works like a charm :)
Unless if project is hidden from viewing :-(

Sounds to me like a place for conditional compiler code ... no time to
pull up an example just now; reply if you want more information. Open
up the VBE and enter conditional compile in the help box.
 
G

GS

Charlotte E brought next idea :
It contains XL2007 code, which won't complie under XL2003 - so, I check for
Application.Version, and import the module if Version is > 11.

Works like a charm :)
Unless if project is hidden from viewing :-(

CE

Ok. So what I do is make a 'plugin' addin to my addin, so if your
'core' addin detects >=XL12 then open the 'plugin' addin and have its
menus added to yours. This will allow you to use Workbooks.Open as
normal, and you can store the xlam in the same folder as your xla.
 
P

Peter T

Charlotte,

If the prohect is locked it's tricky to lock & relock. It can be done
without SendKeys but overkill for your purposes. Even if the project is not
locked User's security settings will need to "trust access to VBProject"

There are workarounds, it depends what you are doing but the simplest is
don't fully declare any objects that prefix later version stuff, eg

Dim rng as Range, oRng as Object

If vers <=11 then
set rng = rng.2003-stuff
Else
Set oRng = oRng.2007/2010-stuff
Set rng = oRng
End if

That's contrived just for ideas, maybe different procedures for 2003 / 2007.

Clif,
Sounds to me like a place for conditional compiler code ... no time to
pull up an example just now; reply if you want more information. Open up
the VBE and enter conditional compile in the help box.

Problem is you can't change the conditional arguments after distributing
without access to the project, in effect back to square one.

Peter Thornton
 
C

Clif McIrvin

Peter T said:
Charlotte,
[ ]

Clif,
Sounds to me like a place for conditional compiler code ... no time
to pull up an example just now; reply if you want more information.
Open up the VBE and enter conditional compile in the help box.

Problem is you can't change the conditional arguments after
distributing without access to the project, in effect back to square
one.

Peter Thornton


Ahh .. I see my error now. In my haste I <conveniently> forgot that
conditional directives can test only constants or literals. Thanks for
the catch!
 

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