Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SendKeys Issue

Status
Not open for further replies.

Jake617

Programmer
Aug 31, 2019
1
0
0
US
Hello--

I'm new to VBScript. I am attempting to use sendkeys (or another method) to input text into a desktop application. However, my code is not working properly to input text into the text boxes (user name and password). My code is below:
Code:
Dim objShell
'Set objShell = CreateObject("Wscript.Shell")
Set objShell = WScript.CreateObject("Wscript.Shell")
Set toolkit = CreateObject("Vbsedit.Toolkit")

objShell.run """C:\Users\%USERNAME%\app.exe"""
For Each window In toolkit.TopLevelWindows
  If InStr(1,window.WindowTitle,"App",vbTextCompare)>0 Then
    WScript.Sleep 5000
    window.Click 1000,500
    WScript.Sleep 2000
    objShell.Sendkeys "email address"
    WScript.Sleep 1000
    objShell.Sendkeys "{TAB}"
    'objShell.Sendkeys chr(9)
    WScript.Sleep 1000
    objShell.Sendkeys "password"
    WScript.Sleep 1000
    objShell.Sendkeys "{ENTER}"
    WScript.Quit
  End If
Next

The output of running this code (video) can be found here:
Any ideas for how to get text to properly input?

Thanks so much!
 
VbsEdit Toolkit is quite dumb. And poorly/incorrectly documented.

e.g
[tt]Click method
Sends a mouse click event to the specified window
[/tt]
does not send a click to the specified window. It sends a click to whatever window is visible at the given coordinates

So you firstly need to ensure that

a) the window is visible
b) it is that window that is visible and topmost at the given coordinates

Secondly you need to ensure there is sufficient time for the window to be displayed

So, I'd modify your code as follows:

Code:
[blue]Dim objShell
[COLOR=green]'Set objShell = CreateObject("Wscript.Shell")[/color]
Set objShell = WScript.CreateObject("Wscript.Shell")
Set toolkit = CreateObject("Vbsedit.Toolkit")

objShell.run """C:\Users\%USERNAME%\app.exe"""
[b]WScript.Sleep 5000[/b]
For Each window In toolkit.TopLevelWindows
  If InStr(1,window.WindowTitle,"App",vbTextCompare)>0 Then
    window.Click  [COLOR=green]' 1000,500 comment out  or remove relative coordinates[/color] 
    WScript.Sleep 2000
    objShell.Sendkeys "email address"
    WScript.Sleep 1000
    objShell.Sendkeys "{TAB}"
    [COLOR=green]'objShell.Sendkeys chr(9)[/color]
    WScript.Sleep 1000
    objShell.Sendkeys "password"
    WScript.Sleep 1000
    objShell.Sendkeys "{ENTER}"
    WScript.Quit
  End If
Next [/blue]

And to be honest I don't think you really need all the Sleep commands after the very first one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top