使用IE.Document.getElementById时需要将错误消息作为对象(" info_window")。innerText

时间:2015-02-18 08:33:26

标签: excel-vba web-scraping vba excel

我对VBA编程非常陌生。当我使用GPS坐标(纬度和经度)进行搜索时,我一直在尝试捕获在谷歌地图上弹出的文本。我一直在使用网站www.gps-coordinates.net。

直到现在我已经能够将GPS坐标传递到网站输入框并点击按钮,并在一个框中弹出谷歌地图上的地址。当我尝试使用IE.Document.getElementById("info_window").innerText从框中获取地址时,我收到错误消息

  

运行时错误'424'对象必需

我无法理解代码所指的对象。我已经在互联网上寻找解决方案,但找不到任何面临类似问题的人,所以找不到一个现成的解决方案。代码段如下所述:

Sub GetPlaceName()

    Dim btn As Variant
    Dim IE As Object, Doc As Object
    Dim sDD1 As String, sDD2 As String
    Dim vAdd As Variant
    Dim lat As Integer, longt As Integer, latrow As Integer, longrow As Integer

    latrow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row - 1
    longrow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row - 1
    If latrow <> longrow Then
        MsgBox "The number of Latitudes & Longitudes are not equal. " & vbNewLine & " PleaseCheck...."
    Else
        Range("B2").Select
        For i = 2 To latrow
            Set IE = CreateObject("InternetExplorer.Application")
            IE.Visible = True
            IE.navigate "http://www.gps-coordinates.net/"
            Do
                DoEvents
            Loop Until IE.ReadyState = READYSTATE_COMPLETE
            Set Doc = IE.Document
            Doc.getElementById("latitude").Value = Range("B" & i).Cells.Value
            Doc.getElementById("longitude").Value = Range("C" & i).Cells.Value
            With Doc
                Set elems = .getElementsByTagName("button")
                For Each e In elems
                    If e.getAttribute("onclick") = "codeLatLng(1)" Then
                        e.Click
                        Exit For
                    End If
                Next e
            End With
            Do
                DoEvents
            Loop Until IE.ReadyState = READYSTATE_COMPLETE
            sDD1 = IE.Document.getElementById("info_window").innerText
            vAdd = Split(sDD1, vbNewLine)
            Cells(i, "E").Value = vAdd
            IE.Quit
            Set IE = Nothing
            Set Doc = Nothing
        Next i
    End If

End Sub

我相信一定有一个很小的错误,我无法弄清楚。它对你们中的一些人来说似乎也很愚蠢,但我真的需要帮助才能解决这个问题。希望很快收到回复......提前致谢: - )

1 个答案:

答案 0 :(得分:0)

尝试使用

Set IE = new SHDocVw.InternetExplorer

而不是

Set IE = CreateObject("InternetExplorer.Application")

这从来没有对我有用。