Results 1 to 2 of 2

Thread: How do you send a TAB key to a Web Page

  1. #1

    How do you send a TAB key to a Web Page

    Register for a FREE account, and/
    or Log in to avoid these ads!

    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:

    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 = ""
            Set sZipKmRadius = WebPageInputTag
        End If
        If WebPageInputTag.Name = "tb_radius_miles" Then
            Set sZipMileRadius = WebPageInputTag
    '        WebPageInputTag.Value = "50"
        End If
        If WebPageInputTag.Name = "goto" Then
            WebPageInputTag.Value = "44122"
        End If
    Next WebPageInputTag
    sZipKmRadius.Value = 0
    appIE.SendKeys "{TAB}", True
    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
    End Sub

  2. #2
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Nanaimo, BC, Canada
    Blog Entries
    Excel Version
    Excel Office 365 Insider
    Hi there,

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

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

    appIE.SendKeys "{TAB}", True


    appIE.SendKeys "{TAB}", True
    appIE.SendKeys "{TAB}", True
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: -||- Blog: -||- Forums:
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts