0

I think I am facing the same problem asked by Gaus Shaikh in the link below but I have the feeling he was not understood at that time. I try to spend some other words on this topic to help you guys for your support.

I create another question because, in my opinion Gaus question title could be misunderstood.

To recap, we have a problem dealing with an excel macro that generate a user form. Actually, if the VBA project is protected by password the macro works. The weird point is that, if I just hide the code (Visual Basic Editor-> VBA project properties -> protection -> lock project from viewing -> insert pw -> OK), the macro won't works even if the project is not protected by password.

I'm stuck here. I am not able to find a solution. Have you guys have any idea? (I am using excel 2013)

Thank you in advance!

Gaus Shaikh question: VBA password protected project not opening userform

N.B: Basically, any macro which generates a user form gives " run-time error 50289. Project is protected." As an example, the following macro that generate an empty user form fails if the code is hidden and works if the code is shown.

Public Sub goUserForm()

    Dim mainframe As Object

    'Set main frame
    Set mainframe = ThisWorkbook.VBProject.VBComponents.Add(3)        

    'Show the form
    VBA.UserForms.Add(mainframe.Name).Show

End Sub

To fully understand the issue, try this macro on your worksheet, then hide your code. Save, close and open the document. The macro won't work unless you show your code.

E.K.
  • 65
  • 1
  • 7
  • The code from the old post cannot work as it was presented. So, I think you have to give more information and also some code pieces – Storax Nov 09 '17 at 09:13
  • Thank you for your quick reply Storax. I have edited the post adding more info about the error. – E.K. Nov 09 '17 at 10:18

1 Answers1

1

Ok, now it's clearer. As the VBA project is protected you will not be able to access it even not with code. Any other code will work. So, this will not work

Set mainframe = ThisWorkbook.VBProject.VBComponents.Add(3) 
VBA.UserForms.Add(mainframe.Name).Show

Code not accessing the VBA project like this will work

UserForm2.Hide
UserForm1.Show
Storax
  • 11,158
  • 3
  • 16
  • 33
  • Thank you for your hint Storax. If you have User Forms in you project, your code works. On the other hand, what if you need to create and add a User Form in your project through your macro? How may I change the following lines without access the project and so avoiding the error? `Set mainframe = ThisWorkbook.VBProject.VBComponents.Add(3)` `VBA.UserForms.Add(mainframe.Name).Show` – E.K. Nov 09 '17 at 11:19
  • If you need to add a userform via code you have to unlock the VBA project first. To my knowledge there is no other way. – Storax Nov 09 '17 at 11:35
  • I can protect my code from viewing through coding. But if I disable this protection, run the macro and enable it again automatically the code will be visible until next restart :( – E.K. Nov 09 '17 at 14:32
  • Yes, you are right. Anyway, code protection with a password is not really secure. http://stackoverflow.com/questions/1026483/is-there-a-way-to-crack-the-password-on-an-excel-vba-project – Storax Nov 09 '17 at 14:47
  • Yeah, I guess so. Thank you Storax. – E.K. Nov 14 '17 at 14:09