Search code examples
vbainternet-explorerautomationie-automation

I'm trying to use VBA to click a "Logon" button on a website to automatically logon


I've been trying to submit login credentials automatically with VBA, and I can get the username and password fields to populate, but I can't get the code to click on the logon button. I've tried numerous solutions I've found on here as well as other websites and can't get it to work. Here is my code (I've taken out any attempt to click the button):

Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub iTradeLogIn()

Dim MyHTML_Element As IHTMLElement
 Dim MyURL As String
 On Error GoTo Err_Clear
 MyURL = "https://www.oms.itradenetwork.com/secure/login/logon.cfm?   _Key=8C049059F2DD44009E"
Set MyBrowser = New InternetExplorer
 MyBrowser.Silent = True
 MyBrowser.Navigate MyURL
 MyBrowser.Visible = True
 Do
 Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
 Set HTMLDoc = MyBrowser.Document
 HTMLDoc.all.UserName.Value = "MyName"
 HTMLDoc.all.Password.Value = "MyPassword"
'Code Here to click the button


Err_Clear:
If Err <> 0 Then
 Err.Clear
 Resume Next
End If
End Sub

Here is some of the source code as well where I think the Logon is:

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    document.body.style.overflow = "hidden";

    var tb = new Ext.Toolbar({
        items: [{
            xtype: 'splitbutton',
            id: 'setLanguage',
            text: 'English',

            menu: new Ext.menu.Menu({
            items: [
                    {text: 'English', id: 'EN', handler: changeLanguage},
                    {text: 'Español', id: 'ES', handler: changeLanguage},
                    {text: 'Deutsch', id: 'DE', handler: changeLanguage},
                    {text: 'Français', id: 'FR', handler: changeLanguage},
                    {text: 'Nederlands', id: 'NL', handler: changeLanguage}
                ]
            })
        }, '->', {
            text: 'Logon',
            id: 'Logon',
            disabled: true,
            handler: performLogon,
            formBind: true
        }]
    });

    var logonForm = new Ext.FormPanel({
        labelWidth: 100,
        frame: true,
        title: 'Member Logon',
        id: 'LogonForm',
        bodyStyle: 'padding: 5px 5px 0',
        width: 350,
        defaults: {width: 200},
        defaultType: 'textfield',
        floating: true,
        shadow: 'drop',
        shadowOffset: 15,
        monitorValid: true,
        buttonAlign: 'left',

        items: [{
                fieldLabel: 'User Name',
                id: 'UserName',
                name: 'UserName',
                allowBlank: false,

                maxLength: 50
            },{
                fieldLabel: 'Password',
                id: 'Password',
                name: 'Password',
                inputType: 'password',
                maxLength: 20
            }, new Ext.form.Checkbox({

                fieldLabel: '',
                labelSeparator: '',
                id: 'RememberMe',
                name: 'RememberMe',
                boxLabel: 'Remember Me?',
                style: 'margin-right: 8px',
                checked: getCookie('REMEMBERME') == "true" ? true : false
            })],

        fbar: tb

Solution

  • To perform the click, you only need to call the Click method on the Logon button element like this:

    Call HTMLDoc.all.Logon.Click
    

    However, a problem is, that the website only enables the button after you have entered a username and a password. If you try to click the button immediately after filling username and password, this is too early, the button is still disabled, so you need to wait a moment.

    In order to wait in VBA, you can import the WinAPI Sleep function like this:

    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    

    And then wait for a given number of milliseconds like this:

    Sleep (500)
    

    Combined code:

    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Sub Demo()
      Dim MyURL As String
      Dim MyBrowser As InternetExplorer
      Dim HTMLDoc As HTMLDocument
    
      MyURL = "https://www.oms.itradenetwork.com/secure/login/logon.cfm"
      Set MyBrowser = New InternetExplorer
      MyBrowser.Silent = True
      MyBrowser.Navigate MyURL
      MyBrowser.Visible = True
    
      Do
        DoEvents
      Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
    
      Set HTMLDoc = MyBrowser.Document
      HTMLDoc.all.UserName.Value = "MyName"
      HTMLDoc.all.Password.Value = "MyPassword"
    
      ' First wait 500ms to give the webite enough time to enable the button
      Sleep (500)
      ' then click the button
      Call HTMLDoc.all.Logon.Click
    End Sub