Friday, September 07, 2018

Replacement for SendKeys in VBA

We have an MS-Access 2016 application that runs on a terminal server which we are upgrading from 2008 R2 to 2012. The prelim testing was done on Windows 10 which seemed equivalent, but not so much.

One feature of the app is an auto-test button, which uses VBA to open forms, click buttons, and make selections from drop-down lists.

The portion of the code for the drop-down lists uses the built-in VBA SendKeys to enter some text and press {ENTER} to make a selection; it works on Windows 10 but unfortunately, not on Windows Server 2012.

After a long Google search, I found a simple replacement in one of the msdn blogs:

Public Sub WSSendKeys(T As String)
Dim WshShell As Object
Set WshShell = CreateObject("wscript.shell")
WshShell.SendKeys T, False
End Sub

...the app already has a reference to the Windows Scripting Runtime, so you'll need to add that in order to use this idea.

1 comment:

Ben said...

Once again you have dug up a trick that could be a lifeline to someone who has hit a wall. I hope search engines pick up the tidbits on your site.