Unprotecting VBA Project via VBA code to change code

  • Hi All!


    I am urgently in need of code to do the following:


    I have an Excel file where the VBA Editor is locked from being viewed. A password is used. Assume it is "Harry".


    At some point, the user has to make use of event driven code to input certain data in an InputBox.


    If the data is correct, a line of code is to be deleted from a specific module. The code that I use looks like this:


    When the password is supplied, everything worked fine UNTIL I locked the project from being viewed.


    I now get an error saying that it cannot write to the VBA Editor because it is locked.


    How can I unlock the project with the relevant password (Assume "Harry", as mentioned) so that the code may be deleted,


    AND


    then to lock it afterwards?


    Your speedy assistance will, as always, be sincerely appreciated!


    Kind regards


    Harry

  • My understanding is that the VBA code must be unprotected (prior to macro running) in order to modify it and cannot be unprotected via code. I could be wrong. :)
    Erlandsen http://www.erlandsendata.no/english/vba/vbe/ and
    Chip Pearson have good VBE pages http://www.cpearson.com/excel/vbe.htm "NOTE: In all versions of Excel, the VBProject must not be protected. If it is, these procedures will fail."
    Have you checked the Ozgrid site?

  • Hi Harry,


    Unfortunately, VBA does not expose the VBProject passwords in its object model. Consequently, the only way to achieve what you want is via a workaround.


    The usual approach is to use SendKeys to simulate the keystrokes that would be used to enter the password for the project. You should be aware, however, that this approach is NOT particularly reliable and I certainly wouldn't want to risk its use in a product that I was distributing (which is, I seem to recall, what you intend to do).


    Here is an example of code that I picked up somewhere (originally written by Bill Manville):

    Use it at your own risk!


    HTH

  • Hi Harry,


    As Richie(UK) has pointed out the only way is with SendKeys.
    An accident just waiting to happen, IMHO.


    That said, I have been playing with a set of SendKey commands and although it will unlock the project I don't think you can Lock it again without closing the workbook completely.
    I could be wrong. ;;)


    So even if you could reliably unlock and remove a line of code you would have to close the workbook.
    Therefore killing any upgrade process you were running in mid-stride.
    Not too smart for the user.


    As a suggestion for an alternative approach;
    how about using a property within the workbook that you can store this code in.

    • A hidden named range
    • Custom document property
    • VBA Projects Description
    • Entry in the user registry


    The reduced functionallity would have IF THEN conditional tests around it. No activation code no execution.


    Cheers
    Andy

  • Hi Guys,


    I dislike very much the SendKey-statement and will never suggest it for a business-solution :rambo:


    BTW, why protect the code at all?
    If it for internally use then I think that everyone knows why, "all of a sudden", the solution does not work for the Office's "super-guy" :smokin:


    Solution:
    Unplugg the "super-guy" :tumble:


    From my own findings about 80% of all XL-users have never open the VBA-part or are not aware of it at all :tick:

  • Harry


    I would say no I do not recommend this send keys are dreadful and can be a bad friend at best of time, not downing Richie(uk) codes but Richie did say at your own risk


    I say don’t bother locking codes, I can open then anyway, and many guys I know can, but as Dennis says unless you know how to bust these types of things, the rest of the guys will be glue less and Dennis’ comments is spot on most don’t even know of the VBE.


    My head cheese at work told me I had written a VLOOKUP wrong ?? And they are to dog???


    I would not bother to much – its not so good – but to answer your question not if its locked you can not.


    Open and edit and re lock ????


    Not so easy – Harry you ask lots of questions with security, can I ask kindly whats the big secret to your work?


    Jack

  • Wow guys!


    That is indeed a mouthful! Your usual quick response to a guy in need is indeed warmly appreciated! Thanks!


    Congratulations Richie on being appointed as Moderator. Good going!


    Let me summarise my conclusion: I am going to unprotect the VBA Project.


    I have to agree with all of you as far as the cluelessness of most Excel users as far as the VBA Editor is concerned. As such I heed all the warnings about the SendKeys "thing" and will sometimes in the future try the code, but not now. I did not even check out those sites that you gave me Egad. Thanks anyway.


    So what am I going to do?


    As said, UNLOCK the whole VBA Project, because the code I gave works splendid then. (Andy, you actually initially helped me with the code)


    I would like to look into your idea of hiding the code within the workbook and triggering it somehow. But that I'll do later.


    As far as the "super secretness" of my work is concerned Jack: It is to me, and that is about it, actually. I am providing other brokers with a "tool" with which I have been earning a lot of money recently. The reason for that is simply to get them to help me penetrate the niche target market and in so doing create an awareness out there amongst these clients, who, as history has it, will start talking amongst other potential professionals, who will then buy quicker when I, or the other brokers, contact them in the future.


    That's where I want to get to, but, I only provide a demo version initially. If they do want to buy the thing later, I will unlock it with an "activation key", effectively opening up the Demo as a full program again.


    The protection of the project is simply to try and prevent a clued-up broker from disabling the demo setting without paying me.


    Bit of a long story, but now you know the basic thought pattern surrounding my paranoia!


    Thanks again. all of you!


    Problem solved!


    Harry

  • Re: Unprotecting VBA Project via VBA code to change code


    Hi


    Im confused. Please excuse this novice. How do I go about unlocking a password protected VBA projet in excel using Vb editor?
    What are sendkeys, and how is it used?


    Thank you

  • Re: Unprotecting VBA Project via VBA code to change code


    If you know the password, then in THe VB Editor Tools menu, select
    VBAProjectProperties>Protection. Then delete the password and uncheck the "Lock Project for Viewing" box.

  • Re: Unprotecting VBA Project via VBA code to change code


    Quote from segran

    What are sendkeys, and how is it used?


    Thank you


    The basic advice given in this thread is NOT to use sendkeys


    It is not a reliable method of coding


    As Roy says, just type in the password.

  • Re: Unprotecting VBA Project via VBA code to change code


    Quote from royUK

    If you know the password, then in THe VB Editor Tools menu, select
    VBAProjectProperties>Protection. Then delete the password and uncheck the "Lock Project for Viewing" box.


    Hi


    I have forgotten the password, hence I dont have access.Please help.
    Thank You

  • Re: Unprotecting VBA Project via VBA code to change code


    Remember guys if You do not know the password(s) then You are truly stuck - do not ask for any advise on this subject as OzGrid will not offer any help or advice on 'cracking' or 'hacking' that just would not be OzGrid


    jiuk

  • Re: Unprotecting VBA Project via VBA code to change code


    Hello all,


    I found another way to unlock a VBE project, without using SendKeys.
    I use Win32 API to find the correct windows and manipulate them.


    The joined zip contents the VB6 source code. It's an Word 2000 version, but you can easily adapt it to Excel and/or in VBA ...


    Don't forget to edit the MVBE.bas module to adapt it to your language (don't works if the windows caption are not correct).


    The code is well commented in bad english (like this post :p). So, sorry in advance ...


    Have a fun ;)

  • Re: Unprotecting VBA Project via VBA code to change code


    gg67


    I have deleted this attachment. You are clearly asked not to post Code or tools to break any passwords. In fact it is Rule number 1

    Quote

    No requesting of help to break any passwords Questions asking to break Sheet Protection, Workbook Protection or VBE Protection will be deleted and could get you banned. Supplying code to enable this, will get you banned.

    I would suggest that you read rhe rules & stick to them if you want to continue to use the Forum

  • Re: Unprotecting VBA Project via VBA code to change code


    Hi,


    It's a misunderstanding. My code don't break any password.
    It's just another way to define programmatically the password.



    Greetings
    Jérôme


    p.s.: i can reattach my source ?

  • Re: Unprotecting VBA Project via VBA code to change code


    Quote

    Does the user need to know the Password to use your application?


    Absolutely.
    It's just a more secure way than SendKeys method to set the VBA project password programmatically.


    greetings

  • Re: Unprotecting VBA Project via VBA code to change code


    providing it cannot be abused to crack passwords, then please attach again. I am sure you understand that we have to be careful.

  • Re: Unprotecting VBA Project via VBA code to change code


    No problem, i understand ;)


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!