How do you send a TAB key to a Web Page

Danielrose01

New member
Joined
Aug 10, 2012
Messages
1
Reaction score
0
Points
0
I am re-posting my original question again as I did not see it in the New Post List:

I was able to code a macro that opens IE, navigates to a website, and fills in data into fields on the page.

However, the webpage does not operate correctly. This is because the TAB key was not pressed when going from one field to the next. By this I mean, if I manually enter the data AND use the TAB key to progress from one field to the next, it works. However, when the MACRO fills in the data into the fields, the web page fails.

So, how do you send the TAB key from inside an EXCEL macro. Here was my attempt:

appIE.SendKeys "{TAB}", True


Here is my macro:

Code:
Option Explicit
Sub Control_Web_Access()
Dim appIE As Object 'This is the IE session
Dim sURL As String 'This is the actual URL of the Web Page
Dim sZipMileRadius As Object  'This the number of miles for the radius around the Zip Code
Dim sZipKmRadius As Object  'This the number of miles for the radius around the Zip Code
Dim DisplayApp As Boolean
Dim WebPageInputTag As Object 'each Tab that is labeled as Input
Call GetIE(appIE)
sURL = "some URL here"
DisplayApp = True
Call WebPageSession(appIE, sURL, DisplayApp)
'Hunt for a field:
'Set sZipMileRadius = appIE.Document.getElementsByName("tb_radius_miles")
Set sZipMileRadius = appIE.Document.getElementsByTagName("INPUT")
'If Not sZipMileRadius Then
'    ' fill in first element named "tb_radius_miles", assumed to be the login name field
'    sZipMileRadius(0).Value = "50"
'End If
For Each WebPageInputTag In sZipMileRadius
    If WebPageInputTag.Name = "tb_radius" Then
'        WebPageInputTag.Value = ""
        WebPageInputTag.Click
        Set sZipKmRadius = WebPageInputTag
    End If
    If WebPageInputTag.Name = "tb_radius_miles" Then
        Set sZipMileRadius = WebPageInputTag
        WebPageInputTag.Click
'        WebPageInputTag.Value = "50"
        sZipKmRadius.Click
    End If
    If WebPageInputTag.Name = "goto" Then
        WebPageInputTag.Value = "44122"
    End If
Next WebPageInputTag
sZipKmRadius.Value = 0
sZipKmRadius.Click
sZipKmRadius.Focus
appIE.SendKeys "{TAB}", True
sZipMileRadius.Focus
appIE.SendKeys "{TAB}", True
' sZipMileRadius.Tab
sZipMileRadius.Value = 50
appIE.SendKeys "{TAB}", True
appIE.SendKeys "{TAB}", True
End Sub
Sub GetIE(IESession As Object)
    On Error Resume Next
    Set IESession = CreateObject("InternetExplorer.Application")
End Sub
Sub WebPageSession(IESession As Object, sURL As String, Display As Boolean)
With IESession
    .Navigate sURL
    ' uncomment the line below if you want to watch the code execute, or for debugging
    .Visible = Display
'    .Title = "DSR Made from Excel Macro"
End With
 
' loop until the page finishes loading
Do While IESession.Busy
Loop
End Sub
 

Ken Puls

Administrator
Staff member
Joined
Mar 13, 2011
Messages
2,521
Reaction score
6
Points
38
Location
Nanaimo, BC, Canada
Website
www.excelguru.ca
Excel Version(s)
Excel Office 365 Insider
Hi there,

Sorry for not the delay here, I just realised that this thread was stuck in moderation. :redface:

The Sendkeys command you're using looks correct to me. I wonder if just sending another immediately would help? Maybe convert:

Code:
[COLOR=#3E3E3E]appIE.SendKeys "{TAB}", True

To

Code:
[/COLOR][COLOR=#3E3E3E]appIE.SendKeys "{TAB}", True
[/COLOR][COLOR=#3E3E3E]appIE.SendKeys "{TAB}", True
[/COLOR]
 
Top