MrExcel Publishing
Your One Stop for Excel Tips & Solutions

protect VBA code from inside code


Posted by ananth venkitaraman on March 19, 2001 2:08 PM

How to (password) protect VBA modules from inside code?
I am having a file with some modules of code.
this is used to create another file(with save as)
I need to protect the code in the new file.

thanks


Posted by Dave Hawley on March 19, 2001 4:50 PM

Hi ananth

Not too sure whether it can be done. This code will return 0 for unlocked and 1 for Locked, but it is read only!

MsgBox Application.VBE.ActiveVBProject.Protection


Maybe it will put you on the right track. If you do find a way I would very interested also.

Dave

OzGrid Business Applications

Posted by Ivan Moala on March 20, 2001 5:09 AM


You could try this routine (modified) to protect
OR unprotect the VBA project.

Const BreakIt As String = "%{F11}%TE+{TAB}{RIGHT}%V{+}{TAB}"

Sub Change_VBA_PW()
Dim WB As Workbook
Dim Password As String

Set WB = ActiveWorkbook
Password = "test"
Call SetVBProjectPassword(WB, Password)

End Sub

Sub SetVBProjectPassword(WB As Workbook, ByVal Password As String)
'Needs reference to Visual Basic for Applications Extensibility Library
Dim VBP As VBProject, openWin As VBIDE.Window
Dim wbActive As Workbook
Dim i As Integer

Set VBP = WB.VBProject
Set wbActive = ActiveWorkbook


Application.ScreenUpdating = False

' close any code windows to ensure we hit the right project
For Each openWin In VBP.VBE.Windows
If InStr(openWin.Caption, "(") > 0 Then openWin.Close
Next oWin

WB.Activate

Application.OnKey "%{F11}"
SendKeys BreakIt & Password & "{tab}" & Password & "~%{F11}", True
Application.ScreenUpdating = True
wbActive.Activate
End Sub

Ivan

Posted by Dave Hawley on March 20, 2001 5:32 AM

Ivan, I like this!

I interesting stuff!

OzGrid Business Applications

Posted by alfredo sanchez on March 20, 2001 12:42 PM

Maybe my version of excel is different but, when I "save as" the worksheet my code is still protected with password