VBA Macro与特定网站进行交互

时间:2016-06-09 18:07:02

标签: javascript excel vba macros

我正在尝试让我的宏登录我的网站。到目前为止,我输入了我的用户名和密码,但我对如何“点击”按钮感到茫然。该网站是https://www.aum-inc.com/log-in,登录按钮不是我知道如何激活的那种。它有某种javascript。下面是我的代码,由我需要为这个特定情况更新的两行写的HELP。

Sub AUM()
 'we define the essential variables
Dim ie As Object
Dim pwd, username
Dim button

'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
Set ie = CreateObject("InternetExplorer.Application")
With ie
    .Visible = True
    .Navigate ("https://www.aum-inc.com/log-in")
    While ie.ReadyState <> 4
        DoEvents
    Wend
    Set username = .document.getElementById("cphContent_C001_txtUserName") 'id of the username control (HTML Control)
    Set pwd = .document.getElementById("cphContent_C001_txtPassword") 'id of the password control (HTML Control)
    'HELP Set button = .document.getElementById("ctl00$cphContent$C001$btnPropLogon") 'id of the button control (HTML Control)
    username.Value = "Account1234"
    pwd.Value = "Password1234"
    'HELP button.Click
    While ie.ReadyState <> 4
        DoEvents
    Wend
End With

'insert some sort of code in VBA to find account

Set ie = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

该网址对我不起作用,但这基本上就是你这样做的。

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.Email.Value = "sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
  1. Microsoft Internet Controls

  2. Microsoft HTML对象库