【发布时间】:2017-08-01 20:50:27
【问题描述】:
我正在尝试使用 Excel VBA 在网站的输入字段中输入值:
Dim FileN As Object
Set FileN = ie.document.getElementsByName("MsgNamePattern")
MsgBox (FileN)
If Not FileN Is Nothing And FileN.Length > 0 Then
FileN(0).Value = fileName
End If
这是输入字段的html代码
<input name="MsgNamePattern" onblur="validateMessageName(this)" type="text" size="20">
我制作了一个用于调试的 MsgBox,如果它成功设置了对象,它应该说“对象 HTMLinputelement”,但我不断收到运行时 91 错误,即由于某种原因未设置对象变量。我使用以下代码成功登录网站:
Dim UserN As Object
Set UserN = ie.document.getElementsByName("userid")
MsgBox (UserN)
If Not UserN Is Nothing And UserN.Length > 0 Then
UserN(0).Value = "username"
End If
MsgBox 会返回“object HTMLinputelement”。这是登录输入字段的 HTML:
<input name="userid" class="inputStyle" onchange="document.login.password.focus();" type="text" size="20">
我没有看到我做错了什么,我以为我使用相同的方法登录成功,所以我很困惑为什么登录后搜索字段不起作用。
这是完整的代码:
Sub getComponents()
Dim WebAddressIn As String
Dim ie As Object
Set ie = New InternetExplorer
WebAddressIn = "https://edx.standardandpoors.com/mailbox/jsp/login.jsp"
Set ie = CreateObject("internetexplorer.application")
ie.Navigate2 WebAddressIn
Do While (ie.Busy Or ie.readyState <> READYSTATE_COMPLETE)
DoEvents
Loop
ie.Visible = True
Dim UserN As Object ' MSHTML.IHTMLElement
Dim PW As Object ' MSHTML.IHTMLElement
Dim ElementCol As Object ' MSHTML.IHTMLElementCollection
Do While ie.Busy
Loop
' enter username and password in textboxes
Set UserN = ie.document.getElementsByName("userid")
MsgBox UserN
If Not UserN Is Nothing And UserN.Length > 0 Then
' fill in first element named "username", assumed to be the login name field
UserN(0).Value = "username"
End If
Set PW = ie.document.getElementsByName("password")
' password
If Not PW Is Nothing And PW.Length > 0 Then
' fill in first element named "password", assumed to be the password field
PW(0).Value = "password"
End If
Do While ie.Busy
Loop
Set ElementCol = ie.document.getElementsByName("submit")
MsgBox ElementCol
For Each btnInput In ElementCol
If btnInput.Value = "*Sign In" Then
btnInput.Click
Exit For
End If
Next btnInput
Do While ie.Busy
Loop
Do While (ie.Busy Or ie.readyState <> READYSTATE_COMPLETE)
DoEvents
Loop
Do Until ie.readyState = 4
Loop
Dim fileName As String
fileName = Format(Now(), "yyyyMMdd") & "_SPGSCI_PRE_STD.TXT"
Dim FileN As Object ' MSHTML.IHTMLElement
Dim SearchBox As Object ' MSHTML.IHTMLElementCollection
Do While ie.Busy
Loop
ie.Visible = False
ie.Visible = True
'Modified to add Tehscript's edit
'Set FileN = ie.document.getElementsByName("MsgNamePattern")
Do
On Error Resume Next
Set FileN = ie.document.getElementsByName("MsgNamePattern")(0)
Loop Until Err.Number <> 91
MsgBox FileN
If Not FileN Is Nothing And FileN.Length > 0 Then
FileN(0).Value = fileName
End If
Do While ie.Busy
Loop
Set SearchBox = ie.document.getElementsByTagName("a")
For Each l In SearchBox
If l.href = "javascript:myFunction('/mailbox/jsp/MBIList.jsp')" Then
l.Click
Exit For
End If
Next l
Do While ie.Busy
Loop
结束子
【问题讨论】:
-
与您的问题无关(我认为这发生在
Set行),但如果UserN是Nothing,If Not UserN Is Nothing And UserN.Length > 0 Then将崩溃 - 因为Nothing不会有Length财产。 -
ie.document设置了吗? FWIW 除非UserN对象有一个 default 属性 返回 [可以表示为] 一个字符串,否则MsgBox调用也会爆炸:删除括号,它们会强制参数被评估并作为一个值传递..应该是MsgBox UserN.Value。 -
@Mat's Mug 我编辑了 MsgBox 但它似乎没有改变任何东西(仍然输出对象...) 我如何确保设置了 ie.document?我还将我的整个代码添加到问题中。
标签: html vba excel internet-explorer