Send Key command in VBA

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

Who is Participating?
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.


RobOwner (Aidellio)Commented:
it related to how you load the form, either modal or modeless.

see this for a better explanation than I can give:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.