+ Reply to Thread
Results 1 to 4 of 4

vba to lock/protect vba

Hybrid View

Ratso vba to lock/protect vba 06-04-2020, 03:30 PM
MRUTOR Re: vba to lock/protect vba 06-05-2020, 02:35 AM
Ratso Re: vba to lock/protect vba 06-05-2020, 10:04 AM
Ratso Re: vba to lock/protect vba 06-05-2020, 10:45 AM
  1. #1
    Forum Contributor
    Join Date
    06-30-2014
    Location
    London, England
    MS-Off Ver
    365 & 2010
    Posts
    107

    vba to lock/protect vba

    Hello you beautiful people,

    I hope you all are keeping well.

    Excel Forum has been my go to point when I get lost so all I can do is thank you all.

    The reason I am posting today as this has got me stumped?

    I have a master workbook that creates multiple workbooks. In these workbooks there is a worksheet change timestamp.

    What I am trying to achieve is to password protect the vba from viewing/amending.

    I have never used sendkeys before and the below code isn't working for me?


    Sub Test()
    
    SendKeys "%{F11}"
    SendKeys "%TE"
    SendKeys "{TAB 9}"
    SendKeys "{RIGHT}"
    SendKeys "{TAB}"
    SendKeys " "
    SendKeys "{TAB}"
    SendKeys "craig"
    SendKeys "{TAB}"
    SendKeys "craig"
    SendKeys "{ENTER}"
    SendKeys "%{F4}"
    
    End Sub
    I can't f8 the routine so I am unsure why it isn't working as all it is doing is adding the word "craig" to the worksheet. Have I missed something?

    I know that this can be cracked via a HEX editor but these are daily spread sheets and the people it will go to won't bother.

    Is there an alternative way of locking the VBA code or can I make the above work?

    I have attached a sample workbook so you can see my issue.

    Any help will be greatly appreciated as per normal
    Attached Files Attached Files
    Last edited by Ratso; 06-04-2020 at 11:23 PM.

  2. #2
    Registered User
    Join Date
    05-14-2020
    Location
    Mauritius
    MS-Off Ver
    Office 365
    Posts
    97

    Re: vba to lock/protect vba

    Hello,

    I changed your code a bit. Even I do not like the sendkey method, but I don't get any other possibility in my mind to protect the VBA project.
    Now the code is doing what it should. after running the code, the file needs to be saved, closed and reopened to have the protection finalised.
    Sub Test()
    
    SendKeys "%{F11}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "%TE", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "{TAB 9}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "{RIGHT}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "{TAB 2}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys " ", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "{TAB 2}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "craig", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "{TAB}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "craig", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "{ENTER}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    SendKeys "%{F4}", True
    Application.Wait (Now + TimeValue("0:00:02"))
    
    End Sub
    Greetings

    Tor


  3. #3
    Forum Contributor
    Join Date
    06-30-2014
    Location
    London, England
    MS-Off Ver
    365 & 2010
    Posts
    107

    Re: vba to lock/protect vba

    Hello MRUTOR,

    Many thanks for the reply but this does not seem to be working for me either.

    It gets as far as opening VBAProject - Project Properties and stops until I either press OK or Cancel please see jpg below. It then runs after I hit a button

    Attachment 681132

    This is really odd as I have copied your macro to the T and am having no success.

    Does there need to be some kind of setting in VBA that I need turning on?
    Last edited by Ratso; 06-05-2020 at 10:06 AM.

  4. #4
    Forum Contributor
    Join Date
    06-30-2014
    Location
    London, England
    MS-Off Ver
    365 & 2010
    Posts
    107

    Re: vba to lock/protect vba

    Hi,

    Whilst Googling I came across the below code which seems to work

    Lock/Unlock VBA Project programmatically by johnske

    Option Explicit
    Sub LockVBAProject()
         With Application
               '//execute the controls to lock the project\\
               .VBE.CommandBars("Menu Bar").Controls("Tools") _
                           .Controls("VBAProject Properties...").Execute
               '//activate 'protection'\\
               .SendKeys "^{TAB}"
               '//CAUTION: this either checks OR UNchecks the\\
               '//"Lock Project for Viewing" checkbox, if it's already\\
               '//been locked for viewing, then this will UNlock it\\
               .SendKeys "{ }"
               '//enter password (password is 123 in this example)\\
               .SendKeys "{TAB}" & "123"
               '//confirm password\\
               .SendKeys "{TAB}" & "123"
               '//scroll down to OK key\\
               .SendKeys "{TAB}"
               '//click OK key\\
               .SendKeys "{ENTER}"
               'the project is now locked - this takes effect
               'the very next time the book's opened...
         End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Lock and protect formulas in Excel
    By Mamyjoly in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-16-2019, 11:00 AM
  2. lock / protect excel macros
    By manekankit in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2019, 03:59 PM
  3. VBA Lock and protect cells
    By mowens74 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-16-2018, 07:53 AM
  4. protect and lock rows
    By tkraju in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2014, 11:46 PM
  5. Protect / Lock VBA Project!
    By Sherif Morsi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-28-2012, 06:52 PM
  6. lock and protect all but certain cells
    By JediMaster in forum Excel General
    Replies: 5
    Last Post: 08-20-2010, 09:52 AM
  7. How to Lock/protect VBAproject?
    By JohnSeito in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-20-2007, 07:14 PM

Tags for this Thread

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