Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

get element by name not working in excel vba

this is the HTML code:

<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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Furthermore, there is no getelementbyname, only getelementSbyname, and it returns a Collection, so you should write:

objie.document.getelementsbyname("d")(0).Value = aloha

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...