我正在尝试使用 VBA 和 Internet Explorer 登录网站。这会导致运行时错误 424“需要对象”。
这是我的代码。寻找您的专家建议。
Sub Website()
Dim IE As Object, Doc As Object, UserName As Object, Password As Object, strCode As String
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate "http://appln.ABC.com/index.epl"
Do While IE.ReadyState <> 4: DoEvents: Loop
Set Doc = IE.Document
Set UserName = Doc.getElementById("login_usr")
UserName.Value = "ABCDE"
Set Password = Doc.getElementById("login_password")
Password.Value = "ABCDE"
Set btnLogin = Doc.getElementById("Login")
btnLogin.Click
End Sub
最佳答案
您是否尝试过本示例中提出的方法?
Fill user name and password in a webpage using VBA
我认为你需要
Set IE = CreateObject("InternetExplorer.Application")
在您的代码中,您实际上并未创建对象,您只是声明它。
编辑
也许绕过您的 Doc 对象的使用:
Sub Website()
Dim IE As Object, UserName As Object, Password As Object, strCode As String
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate "http://appln.ABC.com/index.epl"
Do While IE.ReadyState <> 4: DoEvents: Loop
Set UserName = IE.Document.getElementById("login_usr")
UserName.Value = "ABCDE"
Set Password = IE.Document.getElementById("login_password")
Password.Value = "ABCDE"
Set btnLogin = IE.Document.getElementById("Login")
btnLogin.Click
End Sub
在链接的示例中,他们只使用了一个对象,看来您不需要为您正在做的所有事情创建对象。所以,你可能会逃脱:
Sub Website()
Dim IE As Object
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate "http://appln.ABC.com/index.epl"
Do While IE.ReadyState <> 4: DoEvents: Loop
With IE.Document
.getElementById("login_usr").Value = "ABCDE"
.getElementById("login_password").Value = "ABCDE"
.getElementById("Login").Click
End With
End Sub
您还可以为就绪状态添加第二个 do while 循环,如该链接示例中所示。
关于vba - 运行时错误 424 需要对象 - VBA + Internet Explorer = Web 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438636/