Send Key command in VBA

Posted on 2007-07-23
Last Modified: 2013-08-25
I have a program which automates a webbrowser application within a userform. There were a couple of reasons why this was initialy done like that.

The problem is the program is running the user can not do anything else... For example if you click on something else the macro errors out as it can not function.

My question is: Is there a way to tell Excel to continue sending keys and working with that userform even if the user turns to something else?

Below is part of the code to give you an idea:

' Select the product number
    For j = 1 To 15
        SendKeys "{TAB}", True
    Next j
    SendKeys "{ENTER}", True
    ' wait until browser is free
        If Connecting_IB.WebBrowser1.Busy Then 'If Browser is busy waits an extra 1 second
            Application.Wait TimeValue(Now) + TimeValue("00:00:01")
            Exit Do
        End If
    ' Read External Reference
    externalref = Connecting_IB.WebBrowser1.Document.all("CsietInstance_TagNumber").Value
    Cells(i, 2) = externalref

Question by:Aleyna
    LVL 65

    Accepted Solution

    Unfortunately, no.  SendKeys only sends key strokes to whatever window is active.
    You could either use
    AppActivate "Window Tile"

    before each keystroke (the user still can't do anything else though), or you could try something like this, which will load a web page and use the controls on the form, by control name:
    Set IE = CreateObject("InternetExplorer.Application")

    With IE
            .navigate ""
    End With

    'wait a while until IE as finished to load
    Do while IE.busy

    'IE.passwd.Value = "MyUser"
    IE.Document.ALL("passwd").Value = "test"

    If you "view source" on the web page in question, you can get the <name> on the control you want to use, and you should be OK.


    LVL 42

    Expert Comment

    by:Rob Jurd, EE MVE
    it related to how you load the form, either modal or modeless.

    see this for a better explanation than I can give:

    Expert Comment

    Rob Sampson suggested to you the method CreateObject ("InternetExplorer.Application").
    You should find a different way to solve your "problem" by the following Code (to be customized to your cases).

    With Regards,




    Option Explicit
    Private Const UserName As String = "Some Username"
    Private Const PassWord As String = "Some Password"
    Private n As Integer

    Private Sub CommandButton1_Click()
        WebBrowser1.Navigate ...URL...
    End Sub
    Private Sub WebBrowser1_DocumentComplete (ByVal pDisp As Object, URL As Variant)
        If TypeOf pDisp Is WebBrowser Then
            If InStr(pDisp.Document.Title, ".......") <> 0 Then
                If n < 4 Then
                    pDisp.Document.all("userid").Value = UserName
                    pDisp.Document.all("password").Value = PassWord
                    n = n + 1
                End If
            End If
        End If
    End Sub

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
    My experience with Windows 10 over a one year period and suggestions for smooth operation
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now