I have written the following code to unprotect a vba project using Sendkeys. It works by itself. However, if I put ANY code after the Sendkeys lines, the Sendkeys codes will fail.
Sub UnprotectWB2()
Dim WB1 As Workbook, WB2 As Workbook
Set WB1 = ActiveWorkbook
Set WB2 = Workbooks.Open(filename:=WB1.path & "\aaa.xlsm")
WB2.Activate
SendKeys "%{F11}", True 'Open VBA Editor
SendKeys "^r", True 'Set focus to Project List
SendKeys "{TAB}{TAB}", True 'Move to WB2's Projcet
SendKeys "~", True 'Enter to Open Password Dialog
SendKeys "password", True 'Input password
SendKeys "~", True 'Enter - Now Project Openned in VBA Editor
SendKeys "%T", True 'Alt T for Tools Menu
SendKeys "e", True 'Properties
SendKeys "^{PGDN}", True 'Second tab
SendKeys " ", True 'Space to uncheck protection
SendKeys "~", True 'Enter for OK - Done
'''' It works if the code stops here. However, from here I can not add ANY code (such as WB2.Save or WB1.Activate), otherwise the above code will stop to work, as the keys will be all delayed and pressed all over the place sometimes outside of the project window. It seems like any additional code will be executed BEFORE the Sendkeys even when they are written after them. Adding Sleep or Wait before the additional code won't help. Why?
End Sub
Some people who used similar codes here didn't have such problem.
http://www.pcreview.co.uk/threads/success-in-unprotecting-vba-module-using-vba.2071944/
My Excel version is 2010. Is it a new limitation of 2010? Anyway to solve this problem? Thanks.
Sub UnprotectWB2()
Dim WB1 As Workbook, WB2 As Workbook
Set WB1 = ActiveWorkbook
Set WB2 = Workbooks.Open(filename:=WB1.path & "\aaa.xlsm")
WB2.Activate
SendKeys "%{F11}", True 'Open VBA Editor
SendKeys "^r", True 'Set focus to Project List
SendKeys "{TAB}{TAB}", True 'Move to WB2's Projcet
SendKeys "~", True 'Enter to Open Password Dialog
SendKeys "password", True 'Input password
SendKeys "~", True 'Enter - Now Project Openned in VBA Editor
SendKeys "%T", True 'Alt T for Tools Menu
SendKeys "e", True 'Properties
SendKeys "^{PGDN}", True 'Second tab
SendKeys " ", True 'Space to uncheck protection
SendKeys "~", True 'Enter for OK - Done
'''' It works if the code stops here. However, from here I can not add ANY code (such as WB2.Save or WB1.Activate), otherwise the above code will stop to work, as the keys will be all delayed and pressed all over the place sometimes outside of the project window. It seems like any additional code will be executed BEFORE the Sendkeys even when they are written after them. Adding Sleep or Wait before the additional code won't help. Why?
End Sub
Some people who used similar codes here didn't have such problem.
http://www.pcreview.co.uk/threads/success-in-unprotecting-vba-module-using-vba.2071944/
My Excel version is 2010. Is it a new limitation of 2010? Anyway to solve this problem? Thanks.
Last edited: