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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SendKeys doesn't always send keys 1

Status
Not open for further replies.

mrMikee

Technical User
Jun 17, 2005
8
US
I am having problems with the sendKeys command in VB6. I am sending 10 to 20 keystrokes to another program in order to automate several dialog boxes make and selections in a drop down menu. Sometimes it seems to work and sometimes it doesn't work at all. On occasion I've had the VB program send keystrokes back to itself. I've worked both in the VB environment and with compiled files. It's been very inconsistent.

I am using the following syntax:

SendKeys ky$, True

Part of the ky$ string is an Open File dialog. The dialog opens and the filename shows up but then it hangs for about 5 to 10 seconds. Then it completes the keystrokes without problems sometimes including another file open dialog. Everything else seems to always work.

I have SendKeys before without ever having any problems. It's not an elegant solution but has saved me many hours of pounding the keyboard to get things done.

I am using XP Home Edition SP2, if that matters. I have VB6 Professional but I don't think I am using anything beyond the standard capabilities.

Thanks,
-Mike
 
I hit problems with SendKeys myself a while back. I never did get it working the way I wanted but some of the suggestions in thread222-919190 might help you.
 
Thanks Glasgow.

Your thread looks like it contains some very good info. I've already printed it out and added it to my project folder, and will be trying out some of these ideas today. However in the early hours of the morning I did get things to work a little better by rearranging how the sendkey strings are put together and where the pauses are placed. But still, some things that seem to have worked then stop working.

Out of desperation I replaced my old Compaq keybord and disconnected my KVM switch thinking that there could be bios problems. It didn't seem to help, I really didn't think it would, but overall my program has started working better.

Thanks again,
-mike
 
Good luck - if you find a solution, please post your findings!
 
I had this problem also. So I wrote a function 2 ensure that the AppActivate was completed B4 sending keys. Also by using this function, I no longer have 2 guess how long it should Sleep B4 sending the next set of keys.
Code:
Function AppAct(ByVal sAppName As String, ByVal sKeys2Send As String) As Boolean
    Dim oShell
    Dim iC As Integer 'Loop Counter
    Dim nError As Long 'Holds sAppName activated results
    Const iCMax = 240 'Max loop count
    Set oShell = CreateObject("wscript.shell")
    [COLOR=green][b]'Based on the iCMax (240) & Sleep (250 in milliseconds) the following loop will try up 2 60 seconds 2 activate sAppName[/b][/color]
    Do While iC < iCMax
        nError = oShell.AppActivate(sAppName)
        If nError <> 0 Then 'sAppName is active
            Exit Do
        Else 'Unable to activate sAppName
            iC = iC + 1 'inc counter
            Sleep 250 'wait 250 milliseconds
        End If
        DoEvents
    Loop
    If nError = 0 Then 'unable 2 active sAppName
        AppAct = False
    Else 'sAppName activated, OK 2 sendkeys
        oShell.SendKeys sKeys2Send
        AppAct = True
    End If
    Set oShell = Nothing
End Function
Now call this function from your sub routine.
Code:
 If Not AppAct("[i]ApplicationTitle[/i]", "[i]TheKeyStrokesUWant2Send[/i]") Then Exit Sub
If Not AppAct("[i]ApplicationTitle[/i]", "[i]TheKeyStrokesUWant2Send[/i]") Then Exit Sub

Maybe this world is another planet’s Hell.
Aldous Huxley
 
bigtimmin,

I have inexplicably been able to get my code to work, so for now I will play it safe and not change anything due my monday deadline. The code fragment looks interesting and I hope to be able to plug it into my program next week.

Thanks for your help.

-Mike
 
Sendkeys is dodgy at best. You should try using SendMessage a WM_SETTEXT instead. Play around with Spy++ to get the control classes so that you can find the window handles needed.

You can find sendmessage at allapi.net

David
 
>SendMessage a WM_SETTEXT

WM_SETTEXT is an unlikely candidate for a SendKeys replacement
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top