Link to home
Start Free TrialLog in
Avatar of sai sai
sai sai

asked on

send keys not working in vba

Code:  
Private Sub CommandButton1_Click()
    
    
    Dim IE As Object
    Dim objPage As Object
    Dim wshshell As Object
    
    
    Set IE = CreateObject("InternetExplorer.Application")
    
    
    URL = "***"
    IE.Visible = True
    IE.Navigate URL
    
    While IE.busy
    DoEvents
     Wend
     
     
     Set wshshell = CreateObject("WScript.Shell")
    
     
    newHour = Hour(Now())
    newMinute = Minute(Now())
    newSecond = Second(Now()) + 30
    waitTime = TimeSerial(newHour, newMinute, newSecond)
    Application.Wait waitTime
    
        
    wshshell.SendKeys "{F3}"
    
    
    wshshell.SendKeys "n"
    wshshell.SendKeys "a"
    wshshell.SendKeys "m"
    wshshell.SendKeys "e"
    
    wshshell.SendKeys "{tab}"
    wshshell.SendKeys "{tab}"
    wshshell.SendKeys "test"
    
    wshshell.SendKeys "{F3}"
    wshshell.SendKeys "#"
    wshshell.SendKeys "3"
    
End Sub

Open in new window


here I am trying to automate a web form, as the html code is hidden I am not able to use getelememtbyid methods.
And also I do not have access to spy++, so I an not use windows API calls.

And I am thing of using wscript object in vba .

In the code I am trying to do this:
Launch IE
Open the URL
now comes the problem, there are two dropdown objects
one is 'name' and other 'service #'
inorder to fill these dropdown, I have given find(F3) option to search for the dropdown name. and then pass the value test.

but, when I try running the code. It is not happening as expected.
Pls guide me.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Try adding ",True" after each Sendkeys. (no quotes)
what URL are you contacting?

You can clean up your code by sending multiple key values.  I would also add the True second parameter of the sendkeys method.
    wshshell.SendKeys "name", True

Open in new window

You might also have to add some delays between actions.
If you really can't use IE object methods directly, and do not get SendKeys to work (which is likely, it is flaky und unreliable), maybe switching to automation script languages like AutoIt could be necessary.
what browsers do you have?
Avatar of sai sai
sai sai

ASKER

i have tried adding  'true'. Still, it is not working.
could you pls write the sample code as to how to add delay in between, I have tried adding sleep. but, it does not work.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sai sai

ASKER

The solution worked like a magic. Thanks tons :)