0

I am currently working on a VBA-Project where I want to protect the Code with a password.

Furthermore, I want to use the following lines of code:

Public Sub main()

'Dim CodeModuleSoure As Variant
Dim CodeModuleDestination As Variant


With Application.VBE.ActiveVBProject.VBComponents
'Set CodeModuleSoure = Nothing
Set CodeModuleDestination = .Item(.Count)
CodeModuleDestination.codemodule.addfromstring "Private Sub Worksheet_Activate()" _
                                                & vbCrLf & "Call RefreshRibbon" _
                                                & vbCrLf & "End Sub"
End With

End Sub

This basically adds

Private Sub Worksheet_Activate()
Call RefreshRibbon
End Sub

to the code.

As this does not work while the project is still protected I had a "little" research and found this: Is there a way to crack the password on an Excel VBA Project

Lastly I created a function where I first call the "crack" and then the "code-writer":

Sub otherfuncts()
unprotected
Call Sheet4.main
End Sub

When I try to run the otherfuncts function with a Button it gives me this Error: Error

If I open the VBA-Project it actually is "cracked" and if I rerun the code after viewing the Project the button does not give me an error. Is there a solution that it works in the first try?

Community
  • 1
  • 1
C. Henke
  • 151
  • 1
  • 13
  • Could you not just use `Private Sub Workbook_SheetActivate(ByVal Sh As Object)` in the `ThisWorkbook` module? It will fire on all sheets. – Darren Bartrup-Cook May 03 '17 at 07:56
  • So i should add the sub to the `ThisWorkbook`module? With what effect? – C. Henke May 03 '17 at 08:11
  • Without knowing the full reason for adding the code after the project is finished I assume you want to run `RefreshRibbon` whenever a sheet is activated even if the sheet is added at run-time. `Workbook_SheetActive` will fire whenever any sheet in the workbook is activated. If you want it to fire only on new sheets add a `Select Case` statement to ignore any specific sheets. – Darren Bartrup-Cook May 03 '17 at 08:16
  • I think i need to re-ask the question: I have got a file which is protected by a password. With this file i want to create another file by adding code. This does not work because my file is protected by a password. So i was thinking to crack the password and then create the other file. – C. Henke May 03 '17 at 08:24
  • I find it hard to believe that, in a security-conscious environment, where you password-protect workbooks, worksheets and code (in the vain hope that your data are protected against anything but accidental viewing) people would give you access to the VBA project, which entails the very real risk that hackers take control of your entire system and install whatever they like. I urge you to consider the implications of allowing access to the VBA project for the purpose of enhancing security. It does the opposite. – Variatus May 03 '17 at 09:54

0 Answers0