PDA

View Full Version : Send Keys does not work in my remote application



thitetaf
03-19-2009, 05:47 AM
Hello,

I am using the SendKey function in VBA excel and it does not send the key in my remote application (for instance the Windows Calculator) but in the Code itself.

Below is my code and I don't really see the problem

Thanks in advance

Thibault

Sub LookFor()

Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")

WshShell.Run "calc"
WshShell.AppActivate "Calculator"
WshShell.SendKeys "1"

End Sub

CreganTur
03-19-2009, 05:59 AM
Welcome to the forum- it's always good to see new members.

SendKeys is a notorious function that has many problems. With sendkeys, the keypress is sent to whatever window has focus. Your keypress might be going somewhere other than the calculator.

Why are you using the calculator application instead of doing the calculations within Excel itself?

thitetaf
03-19-2009, 06:28 AM
Hello

Thanks for that prompt reply !!

Actually I am not working on a Calculator function since it could be performed under Excel as you mentioned

I wanted to use the sendkeys({tab}) to access a field on an internet webpage. If my code open IE successfully, the sendkeys({tab}) did not work.

I first thought it would be a problem with my IE, that why I decided more basic stuff like the calculator one.

I don't really see how to fix this problem

Thanks for your help

Thibault

thitetaf
03-19-2009, 06:36 AM
I may have found a solution with imposing a wait constraint... but I don't really like this manner... Do you have any better ideas ?

Kenneth Hobs
03-19-2009, 06:39 AM
If you are using internet explorer, why not use its object model methods?

Sub LookFor()
Shell "Calc", vbNormalFocus
AppActivate "Calculator", True
SendKeys "1"
End Sub

CreganTur
03-19-2009, 06:43 AM
Do you have any better ideas ?

Only one- if you can identify the objects on the webpage, then you can use VBA to directly write into them.

You will need a reference to Microsoft Internet Controls to run the following code, which will open the USPS zip-code search webpage, and then load values into some of the text boxes:
Sub DataStuff()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://zip4.usps.com/zip4/welcome.jsp"
ie.Visible = True
While ie.busy
DoEvents
Wend
ie.Document.All("address2").Value = "123 Carraige Way"
ie.Document.All("city").Value = "Shreveport"
ie.Document.All("state").Value = "CA"
ie.Document.All("zip5").Value = "9021"
End Sub

You can try to determine the name of webpage objects by clicking, in IE, View->Source. You'll have to pick through the source code to try and locate the objects.

HTH:thumb