Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15610
  • Last Modified:

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
    Do
        DoEvents
        If Connecting_IB.WebBrowser1.Busy Then 'If Browser is busy waits an extra 1 second
            Application.Wait TimeValue(Now) + TimeValue("00:00:01")
        Else
            Exit Do
        End If
    Loop
    ' Read External Reference
    externalref = Connecting_IB.WebBrowser1.Document.all("CsietInstance_TagNumber").Value
    Cells(i, 2) = externalref
Thanks,



Matt
0
Aleyna
Asked:
Aleyna
1 Solution
 
RobSampsonCommented:
Matt,
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
        .left=200
        .top=200
        .height=400
        .width=400
        .menubar=0
        .toolbar=1  
        .statusBar=0
        .navigate "http://www.hotmail.com/"
        .visible=1
End With

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

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

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.

Regards,

Rob.
0
 
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:

http://msdn2.microsoft.com/en-us/library/39wcs2dh(VS.80).aspx
0
 
sp10635Commented:
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,

E.C.

__________________________

Code.

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
                pDisp.Document.all("btnSubmit").Click
                n = n + 1
            End If
        End If
    End If
End Sub
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now