gpt4 book ai didi

vba - 运行时错误 424 需要对象 -- VBA + Internet Explorer = Web 登录

转载 作者:行者123 更新时间:2023-12-04 20:12:06 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com