Vba 使用VB宏登录网站

Vba 使用VB宏登录网站,vba,excel,Vba,Excel,我正在尝试使用宏从站点中提取数据。下面是我的代码。它会填充ID和密码,但不会实际登录到该站点。需要帮助以确定实际错误的位置。然后我可以慢慢地进入正确的位置提取报告 Dim HTMLDoc As HTMLDocument Dim oBrowser As InternetExplorer Sub HRALogin() Dim oHTML_Element As IHTMLElement Dim sURL As String On Error GoTo Err_Clear sURL = "websi

我正在尝试使用宏从站点中提取数据。下面是我的代码。它会填充ID和密码,但不会实际登录到该站点。需要帮助以确定实际错误的位置。然后我可以慢慢地进入正确的位置提取报告

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

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "website" 'removed actual website due to sensitivity
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.UserName.Value = "abcdefg"
HTMLDoc.all.Password.Value = "1234abcd"

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

' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If

End Sub
Dim HTMLDoc作为HTMLDocument
作为InternetExplorer的Dim oBrowser
副秘书长()
尺寸oHTML\U元件作为IHTML元件
作为字符串的Dim sURL
错误时转到错误清除
sURL=“website”'由于敏感性,删除了实际网站
Set oBrowser=新的InternetExplorer
oBrowser.Silent=True
oBrowser.timeout=60
船首导航
oBrowser.Visible=True
做
'等待浏览器加载
循环直到oBrowser.ReadyState=ReadyState\u完成
设置HTMLDoc=oBrowser.Document
HTMLDoc.all.UserName.Value=“abcdefg”
HTMLDoc.all.Password.Value=“1234abcd”
对于HTMLDoc.getElementsByTagName(“输入”)中的每个oHTML_元素
如果oHTML_Element.Type=“login”,则oHTML_Element.Click:退出以获取
下一个
“oBrowser.Refresh”根据需要刷新
错误清除:
如果错误为0,则
呃,明白了
下一步继续
如果结束
端接头
这是:

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
    If oHTML_Element.Type = "login" Then oHTML_Element.Click: Exit For
Next
相当于:

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
    If oHTML_Element.Type = "login" Then
        oHTML_Element.Click
    End If
    Exit For
Next
i、 e.的
退出不受
If
的控制。那可能是你的问题

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

Do 
'Wait till the Browser is loaded
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
另外,在单击login后设置延迟,以便它正确加载页面,然后执行其余操作