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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,214,875
Messages
6,122,046
Members
449,063
Latest member
ak94

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