Sendkeys Won't Work If Followed By Any Other Code, Why?

Ceeyee

Board Regular
Joined
Feb 2, 2011
Messages
164
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.
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Just seen your post - I also experienced recent Sendkey failure in 2010 after 6 month's of successful running - have you found a solution? Have you tried DoEvents and/or followed by a Msgbox to ensure all parallel processing threads have completed. My problem is that Sendkeys has stopped acting on the correct VB project window.
 
Upvote 0
Spent a couple of hours investigating this problem this morning and the only way I have found is to insert a Stop in the VB code and call the code as a self contained Sub. Not a good solution but it works whereas MsgBox, DoEvents and Wait do not work. This behaviour definitely appears to be due to recent Excel 2010 changes and I am surprised that this is the only error report I have found so far. I will post details if I find a proper solution but your Tab/Tab code is likely to be specific to what you have running in Excel. Also, there is a much better solution in Unprotecting VBA Project Password (Using a password that you know) which I have used with great success but not always easy to graft into existing code and I have struggled with selecting the correct version of Excel (I have 2003 and 2010 installed) when creating a new instance.
 
Upvote 0
cracked it - call your code as a separate sub with parameter of password
after call put doevents so that the main processing waits until Sendkeys parallel sub-processing completes - looks to me like the Sendkey Wait is not working now in Excel 2010.
e.g.
UnprotectWB2 passwd
Doevents
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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