Discussion:
VB SendKeys Alternatives
(too old to reply)
Bob
2014-11-09 19:28:09 UTC
Permalink
Looking for better method than SendKeys in VB to open, control and close a
program to insure the commands are getting to the right application.

I was mediocre programmer in VB6 way back when. I can get around using VB
scripting and VBA after some review. I'm retired now and have not kept up
with the latest and greatest like PowerShell but I have played with it.

I have a friend that runs a small business and I maintain his systems and
networks. Recently he had to upgrade the software which he runs his business
on - Mitchell 1, if you're familiar with that. In the past, you could
perform a manual backup from designated clients whenever the client was
closed out for the day. With the upgrade, that capability is now only
available from the server and cannot be automated from within the program -
only manually. The server is not readily available for someone to get access
to and perform the backup - and I would want them to...

I've made up a script that performs an automated manual backup of the
database at a specific time right after closing time from the server. That
backup is copied to a local drive, a NAS and to an offsite location.
Additionally, backup images of the server and clients are made every night.
That pretty well covers being able to recover the server, clients and
database for getting things working again fairly quickly but is not user
friendly recovery method for the typical user.

The manual backup file is a special type of backup that a user can initiate
easily should the database be corrupted and is done from within the software
the user is familiar with. So it's important to have that backup done each
night. The server is not easily accessed to prevent any user from
accidentally using that computer but the owner does have access.

The server stays in a pretty static condition 99% of the time but it's that
1% that bites you in the butt - and has using the SendKeys method. Mitchell
is not about to provide an API for me to use so I'm looking for a fool proof
method that will work even if the focus of the program is lost during
execution of the backup. Have done a lot of research and tried AutoIt and
several iterations of a VB API replacement for SendKeys which I could not
get to work.

Anyone have an example VB program or a site reference for a script, program
or a PowerShell cmdlet (I'll learn it...) that uses a bullet-proof method
for opening an instance of a program, send (key) commands in response to the
programs pop-up windows and then close out the program (gracefully) no
matter what else may be going on with other applications or operating
system?

I'll keep looking and trying but if you have licked this problem yourself,
I'd like to read what you did.

Thank you,

Bob S.
Todd Vargo
2014-11-09 23:26:13 UTC
Permalink
Post by Bob
Looking for better method than SendKeys in VB to open, control and close
a program to insure the commands are getting to the right application.
I was mediocre programmer in VB6 way back when. I can get around using
VB scripting and VBA after some review. I'm retired now and have not
kept up with the latest and greatest like PowerShell but I have played
with it.
I have a friend that runs a small business and I maintain his systems
and networks. Recently he had to upgrade the software which he runs his
business on - Mitchell 1, if you're familiar with that. In the past,
you could perform a manual backup from designated clients whenever the
client was closed out for the day. With the upgrade, that capability is
now only available from the server and cannot be automated from within
the program - only manually. The server is not readily available for
someone to get access to and perform the backup - and I would want them
to...
I've made up a script that performs an automated manual backup of the
database at a specific time right after closing time from the server.
That backup is copied to a local drive, a NAS and to an offsite
location. Additionally, backup images of the server and clients are made
every night. That pretty well covers being able to recover the server,
clients and database for getting things working again fairly quickly but
is not user friendly recovery method for the typical user.
The manual backup file is a special type of backup that a user can
initiate easily should the database be corrupted and is done from within
the software the user is familiar with. So it's important to have that
backup done each night. The server is not easily accessed to prevent
any user from accidentally using that computer but the owner does have
access.
The server stays in a pretty static condition 99% of the time but it's
that 1% that bites you in the butt - and has using the SendKeys method.
Mitchell is not about to provide an API for me to use so I'm looking for
a fool proof method that will work even if the focus of the program is
lost during execution of the backup. Have done a lot of research and
tried AutoIt and several iterations of a VB API replacement for SendKeys
which I could not get to work.
Anyone have an example VB program or a site reference for a script,
program or a PowerShell cmdlet (I'll learn it...) that uses a
bullet-proof method for opening an instance of a program, send (key)
commands in response to the programs pop-up windows and then close out
the program (gracefully) no matter what else may be going on with other
applications or operating system?
I'll keep looking and trying but if you have licked this problem
yourself, I'd like to read what you did.
No such thing as bullet proof when it comes to SendKeys. The best you
can do is present a warning message at the start of the script. You
could throw in extra AppActivate statements before each SendKeys
statement, but you are still at the mercy of the user following
instructions.

x = MsgBox ("To prevent errors, DO NOT touch " & _
"keyboard or mouse until backup is complete." & _
vbNewline & "Ready to to start the backup " & _
"now?", 36, "Mitchell 1 BACKUP")
If x = 7 Then Wscript.Quit

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc"
WScript.Sleep 500

WshShell.AppActivate "Calculator"
WScript.Sleep 100
WshShell.SendKeys "1{+}"
WScript.Sleep 500

WshShell.AppActivate "Calculator"
WScript.Sleep 100
WshShell.SendKeys "2"
WScript.Sleep 500

WshShell.AppActivate "Calculator"
WScript.Sleep 100
WshShell.SendKeys "~"
WScript.Sleep 500

WshShell.AppActivate "Calculator"
WScript.Sleep 100
WshShell.SendKeys "*3"
WScript.Sleep 500

WshShell.AppActivate "Calculator"
WScript.Sleep 100
WshShell.SendKeys "~"
WScript.Sleep 2500

WshShell.AppActivate "Calculator"
WScript.Sleep 100
WshShell.SendKeys "(%{f4})"

MsgBox "Backup is complete.", 64, "Mitchell 1 BACKUP"
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
Bob
2014-11-10 16:33:22 UTC
Permalink
(sniped everything....for brevity)

Todd,

Thanks for the confirmation, suggestions and your example script. I will
certainly give that a try and then see if I can bomb it while the script is
running to test it.

You're probably very well aware of this but Task Scheduler was one of the
culprits in making the program lose focus after my script initialized.
AppActivate would not maintain nor force the program back into focus.

I tried various things but what worked in the end was using two scripts.
The first one is fired off by Task Scheduler and then Task Scheduler is
done. That first script now fires the second (main) script that does the
work. With Task Scheduler out of the way, I've not had any problems with
the program losing focus while being initialized as was the case when I used
just the main script.

I'm sure this is probably a well known trick but I discovered it myself just
a few days ago thru trial and lots of error.....

Thanks for your time and taking the effort to explain things. Very helpful.

Bob S.

Loading...