vba 按名称获取元素在excel vba中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39948236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get element by name not working in excel vba
提问by Hamza Khan
this is the HTML code:
这是 HTML 代码:
<table>
<tr><td>Date:</td><td><input type='text' name='d' value=''><br>(Example: Oct. 8)</td></tr>
<tr><td>Teams:</td><td><input type='text' name='away' value=''> at <input type='text' name='home' value=''><br>(Example: Florida at Buffalo)</td></tr>
<tr><td>Time:</td><td><input type='text' name='t' value=''><br>(Example: 7:00 PM)</td></tr></table><br><input type='submit' value='Submit'>"
I am using this code to:
我正在使用此代码:
Sub extractdata()
Dim x As Long
Dim objie As Object
Set objie = CreateObject("internetexplorer.application")
objie.Top = 0
objie.Left = 0
objie.Width = 800
objie.Height = 600
objie.Visible = True
objie.navigate "http://www.dailynhlgoalies.com/loadSchedule.php"
x = 1
Dim aloha As String
Dim aloha1 As String
Dim aloha2 As String
Dim aloha3 As String
aloha = Sheet1.Range("A" & x).Value
aloha1 = Sheet1.Range("B" & x).Value
aloha2 = Sheet1.Range("C" & x).Value
aloha3 = Sheet1.Range("D" & x).Value
Application.Wait (Now + #12:00:02 AM#)
Do
DoEvents
If Err.Number <> 0 Then
objie.Quit
Set objie = Nothing
GoTo the_start:
End If
Loop Until objie.readyState = 4
objie.document.getelementbyname("d").Value = aloha
objie.document.getelementbyname("away").Value = aloha1
objie.document.getelementbyname("home").Value = aloha2
objie.document.getelementbyname("t").Value = aloha3
End Sub
but it is not working
但它不起作用
回答by z32a7ul
You close the application on the lines
你关闭应用就行
objie.Quit
Set objie = Nothing
and try to use it later
并尝试稍后使用它
objie.document.getelementbyname("d").Value = aloha
you get the error because you closed IE and set the pointer to Nothing
您收到错误是因为您关闭了 IE 并将指针设置为 Nothing
Furthermore, there is no getelementbyname, only getelementSbyname, and it returns a Collection, so you should write:
此外,没有getelementbyname,只有getelementSbyname,而且它返回一个集合,所以你应该写:
objie.document.getelementsbyname("d")(0).Value = aloha
回答by Tim Williams
It's getElementsByName
(note the "s") and it returns a collection of matching elements, not a single element, so you should do something like:
它是getElementsByName
(注意“s”)并且它返回匹配元素的集合,而不是单个元素,因此您应该执行以下操作:
objie.document.getelementsbyname("d")(0).Value = aloha
for example
例如