SendKeys to unlock a VBA Project placing password inside an already unlocked project module

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
429
Office Version
  1. 2016
Platform
  1. Windows
I'm using the following macro to unlock my VBA projects when the workbook is opened. However if I already have another vba project unlocked, this macro will sometimes just place the password inside module and do nothing.

Is there a way to designate specifically which project should be unlocked?

Code:
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Private Sub Unlock_Project()
With Application
        .SendKeys "%{F11}", True 'Alt-F11
        Sleep 500
        
        .SendKeys "^r", True         'CTRL+R
        Sleep 500
        
        .SendKeys "~", True          'enter
        Sleep 500
        
        .SendKeys ThisWorkbook.Password_M, True 'ThisWorkbook.Password_M is given a value when an earlier function is called
        Sleep 500
        
        .SendKeys "~", True 'enter
        Sleep 500
        
        .SendKeys "%q", True 'quit vba editor
End With
End Sub
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top