VBA to unlock the module that the VBA is running in!

dawsona

Board Regular
Joined
Jul 28, 2002
Messages
66
I had a question about unlocking a VBA project answered recently but now it has grown another step.

I have a workbook that has two modules in it. The first module is code to copy the second module into another workbook.

Both the source file and the destination workbook have their VBA projects password protected which means that I have to unlock the source before it will let me copy the module to the receiving workbook.

Code so far (which obviously dosen't work) is below:

Sub aaa()

' Copy module in

Dim wbproj As Object
Set wbproj = ActiveWorkbook.VBProject
Application.SendKeys "pword1", True
Application.SendKeys "~", True
wbproj.VBE.selectedvbcomponent.Activate

Dim mySource As String
Dim myDestination As String

mySource = ThisWorkbook.Name
myDestination = ActiveWorkbook.Name

Dim wbbproj As Object

Set wbbproj = ThisWorkbook.VBProject
wbbproj.VBE.selectedvbcomponent.Select
Application.SendKeys "test", True
Application.SendKeys "~", True

With Workbooks(mySource)
modname = .Path & "code.txt"

Application.DisplayAlerts = False

.VBProject.VBComponents("AD_Macros34").Export modname
End With
Workbooks(myDestination).VBProject.VBComponents.Import modname
Kill modname

End Sub

The problem appears to be the line:

Set wbbproj = ThisWorkbook.VBProject

as it is not unprotecting the source module.

Can anyone help?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
On 2002-09-18 07:44, dawsona wrote:
I had a question about unlocking a VBA project answered recently but now it has grown another step.

I have a workbook that has two modules in it. The first module is code to copy the second module into another workbook.

Both the source file and the destination workbook have their VBA projects password protected which means that I have to unlock the source before it will let me copy the module to the receiving workbook.

Code so far (which obviously dosen't work) is below:

Sub aaa()

' Copy module in

Dim wbproj As Object
Set wbproj = ActiveWorkbook.VBProject
Application.SendKeys "pword1", True
Application.SendKeys "~", True
wbproj.VBE.selectedvbcomponent.Activate

Dim mySource As String
Dim myDestination As String

mySource = ThisWorkbook.Name
myDestination = ActiveWorkbook.Name

Dim wbbproj As Object

Set wbbproj = ThisWorkbook.VBProject
wbbproj.VBE.selectedvbcomponent.Select
Application.SendKeys "test", True
Application.SendKeys "~", True

With Workbooks(mySource)
modname = .Path & "code.txt"

Application.DisplayAlerts = False

.VBProject.VBComponents("AD_Macros34").Export modname
End With
Workbooks(myDestination).VBProject.VBComponents.Import modname
Kill modname

End Sub

The problem appears to be the line:

Set wbbproj = ThisWorkbook.VBProject

as it is not unprotecting the source module.

Can anyone help?

In order to get the correct component for your project you will need to close al VBE windows THEN set your VBObject to the

Set wbbproj = ThisWorkbook.VBProject

wbbproj.VBE.selectedvbcomponent.Select
Application.SendKeys "test", True
Application.SendKeys "~", True


Something like this;

<pre/>
Dim OpenWnd As VBIDE.Window
Dim i As Integer

'On Error Resume Next
Set wbproj = Thisworkbook.VBProject

'// Before openning if you have other projects
'// Not protected then you need
'// to close each window to make sure
'// you get the correct project
For Each OpenWnd In wbproj.VBE.Windows
If InStr(OpenWnd.Caption, "(") > 0 Then OpenWnd.Close
Next OpenWnd
</pre>
 
Upvote 0
Ivan

Thanks for your help. The VBIDE.Window wasn't accepted (Excel 97) but it worked okay when I changed this to As Object.

I now have another problem which is very similar to a previous query I located. WoodyHays posted 2002-05-20 "Open VB Editor"

I am having the same problem re opening the editor from the vb code and have tried the code that NateO gave without success. The Application.Goto Reference:="tryThis" does not work for me.

You were in on the end of this one and I wondered if you have any ideas?
 
Upvote 0
Howdy,

Please post your procedure. I've had issues with security settings in Xp, but it should be fine in '97.

If you renamed the procedure, TryThis to another name, change the reference in the goto method to that name.

Note, you don't just unlock the module, you unlock the whole thing, right up until you close the file.... And, I also tweaked the procedure here. The note about not working with XP is assuming you don't modify your VBE/macro security settings.
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,525
Members
449,037
Latest member
tmmotairi

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