+ Reply to Thread
Results 1 to 19 of 19

Protect VB Project using VBA

  1. #1
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Protect VB Project using VBA

    I need to know how to protect a VB project (so that no users can view or edit it) in VBA. The reason is I have a loop that I need to run it in for multiple workbooks.

  2. #2
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Protect VB Project using VBA

    Quote Originally Posted by Eric09 View Post
    I need to know how to protect a VB project (so that no users can view or edit it) in VBA. The reason is I have a loop that I need to run it in for multiple workbooks.
    Here are the steps:

    1. Go to VBA Editor, on the Project Explorer, right click on the VBA Project Name.
    2. Select VBA Project Properties
    3. Go to Protection Tab
    4. Check the "lock project for viewing"
    5. Input passwords
    6. Click OK

    Hope this would help you

  3. #3
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    Actually, I need to know how to do this in VBA. I cannot go through hundreds of workbooks and set it manually. Using the same password is fine.

  4. #4
    Forum Contributor
    Join Date
    02-24-2011
    Location
    Belfast, N. Ireland
    MS-Off Ver
    Excel 2010
    Posts
    103

    Re: Protect VB Project using VBA

    Eric,

    Just a warning, if your VBA code tries to change itself or a module etc then it will throw up errors and not work correctly when locked. I found that out the hard way.

    Mowgli

  5. #5
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    I will be locking them when I finish creating and modifying them. I will not modify on the fly! When I modify a workbook, I delete it and re-create it. Locking the VB project will be the last step, but I need to do it in code.

    EDIT: I just tested it and when the VB project is protected it does not affect the functionality of the macro I have written. It still works fine.
    Last edited by Eric09; 09-24-2012 at 07:51 AM.

  6. #6
    Forum Contributor
    Join Date
    02-24-2011
    Location
    Belfast, N. Ireland
    MS-Off Ver
    Excel 2010
    Posts
    103

    Re: Protect VB Project using VBA

    Eric,

    So far as I am aware there is no method of automatically locking the VBA project using code. I have seen "sendkeys" suggested as an option however the general concensous seems to be avoid these like the plague. Sorry that I can't be of more help but maybe someone else in the Forum will know differently?!?

    The error I was talking about earlier was when the code runs it may sometimes change depending on items in the sheet or user input. This is what caught me out, after testing my code and having it run perfectly I locked the VBA project, ran the Macro and... Error

  7. #7
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    I don't know. Maybe there is a workaround. I just need to hide the macros behind the sheet so that the users I email the workbooks to will not be able to view or edit them. There are no modules and I have no problem with them creating their own modules or macros, I just don't want them playing with mine. My macros (in the sheets) are there to prevent getting crappy, ambiguous data back. And I need it to stay that way...if at all possible.

  8. #8
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Protect VB Project using VBA

    Eric,

    Do the workbooks that are affected by the loop are new or existing workbooks?

  9. #9
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    I create the workbooks and the macros in them using VBA (a split). They are all in the same folder, hundreds of them. So, I am not able to open them all manually and modify them.

  10. #10
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Protect VB Project using VBA

    Are those workbooks in the same format or all the same?
    Is it possible for you to use a template with a macros on it (VB Project password protected)?
    Then everytime you run the macros "using VBA (a split)", you just open that template then do the things.

  11. #11
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    I could but I need the macros in the workbooks to work. There are dependent drop downs and Cols. D E and F and the macros are designed to reset the values in E if the user changes the choice in D. As far as I know, there is no worksheet function that will automatically delete values based on a cell change. Therefore the macros have to be in the workbook the users get.
    Last edited by Cutter; 09-24-2012 at 07:32 PM. Reason: Removed whole post quote

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Protect VB Project using VBA

    sendkeys method http://www.mrexcel.com/forum/excel-q...via-macro.html
    (may or may not be reliable)

    api method http://www.standards.com/Office/SetV...tPassword.html
    should be more reliable
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Protect VB Project using VBA

    Good link Joseph, I hadn't seen that method before

  14. #14
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    Thanks Joseph.

    May I ask a stupid question, re: the method in the second link? Why is the code to perform such a simple task as to password protect a little bit of VBA code a mile long?

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Protect VB Project using VBA

    because there is nothing in the excel object model that allows you to do it-presumably following the foolish notion that that would make project security easy to break (which it is anyway of course). therefore you have to trigger dialogs then get handles to controls and manipulate them programmatically-basically a literal automation of the manual steps. in my opinion it's usually a waste of time-if someone is likely to tamper with your code it's easy to break protection or just disable macros

  16. #16
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Protect VB Project using VBA

    Because it isn't built into Excel, there's no way to do it. It therefore needs doing the hard way, by going through all the steps and identifying the relevant parts by code

  17. #17
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    But, assuming I do get the VBA project protected, is it easy enough for someone who knows nothing at all about VBA or advanced Excel to disable the macros? If the VBA is not protected, all they need to know is how show the developer ribbon (a 5 minute Google search) and the location of the 'delete' key.

  18. #18
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Protect VB Project using VBA

    if they can google they can break it ;-)

    why would they though? if you get a workbook back without your code in it just refuse it

  19. #19
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Protect VB Project using VBA

    Ok. At this point, it looks like I will have to do that. Hopefully, they won't have time to bother.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1