Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何使用appIE.Document.Body.innerHTML_Excel_Vba_Geocoding - Fatal编程技术网

Excel 如何使用appIE.Document.Body.innerHTML

Excel 如何使用appIE.Document.Body.innerHTML,excel,vba,geocoding,Excel,Vba,Geocoding,因此,我试图检索给定邮政编码的纬度和经度,并试图使用VBA将其放入excel工作表中。我的代码如下: Private appIE As Object Function GeoCode(sLocationData As String) As String '//Dont want to open and close all day long - make once use many If appIE Is Nothing Then CreateIEApp '// Creates

因此,我试图检索给定邮政编码的纬度和经度,并试图使用VBA将其放入excel工作表中。我的代码如下:

Private appIE As Object

Function GeoCode(sLocationData As String) As String
'//Dont want to open and close all day long - make once use many
If appIE Is Nothing Then
    CreateIEApp
    '// Creates a new IE App
    '// if = nothing now then there was an error
    If appIE Is Nothing Then
        GeoCode = "Sorry could not launch IE"
        Exit Function
    Else
        '// do nothing
    End If
Else
    '// do nothing
End If
'//clearing up input data
'sLocationData = Replace(sLocationData, ",", " ")
sLocationData = Replace(sLocationData, " ", "+")
sLocationData = Trim(sLocationData)

'//Build URL for Query
sLocationData = "http://maps.google.com/maps/geo?q=%20_" & sLocationData

'// go to the google web service and get the raw CSV data
'// CAUSES PROBLEM AS SPECIFIED BELOW
appIE.Navigate sLocationData

Do While appIE.Busy
    Application.StatusBar = "Contacting Google Maps API..."
Loop

Application.StatusBar = False
On Error Resume Next

'// Parsing
GeoCode = appIE.Document.Body.innerHTML
GeoCode = Mid(GeoCode, InStr(GeoCode, ",") + 1, InStr(GeoCode, "/") - InStr(GeoCode, ",") - 2)

appIE = Nothing
End Function
Google Maps API然后返回一个JSON格式的值,如下链接所示:

然后,我尝试使用
appIE.Document.Body.innerHTML

并为我想要的数据解析该值。但是,当代码点击appIE.Navigate sLocationData时,
系统会提示我保存一个名为“geo”的文件。当保存并打开为.txt文件时,我会得到完全相同的JSON格式的值,但我需要工作表本身中的值

有办法做到这一点吗


提前谢谢

在Firefox-response 610中,这个链接对我不起作用。如果我删除空格和下划线,它就会工作。我不知道IE为什么要下载,可能是某种设置告诉它总是下载JSON而不是呈现JSON。在任何情况下,考虑使用MSXML的HTTP请求,而不是自动化IE.< 设置对MicrosoftXML、v6.0或类似版本(VBE-Tools-References)的引用

Function GeoCode(sLocData As String) As String

    Dim xHttp As MSXML2.XMLHTTP
    Dim sResponse As String
    Dim lStart As Long, lEnd As Long

    Const sURL As String = "http://maps.google.com/maps/geo?q="
    Const sCOOR As String = "coordinates"": " 'substring that we'll look for later

    'send the http request
    Set xHttp = New MSXML2.XMLHTTP
    xHttp.Open "GET", sURL & sLocData
    xHttp.send

    'wait until it's done
    Do
        DoEvents
    Loop Until xHttp.readyState = 4

    'get the returned data
    sResponse = xHttp.responseText

    'find the starting and ending points of the substring
    lStart = InStr(1, sResponse, sCOOR)
    lEnd = InStr(lStart, sResponse, "]")

    GeoCode = Mid$(sResponse, lStart + Len(sCOOR), lEnd - lStart - Len(sCOOR) + 1)

End Function

Sub Test()

    Dim sTest As String

    sTest = GeoCode("400012")

    Debug.Assert sTest = "[ 103.9041520, 1.3222160, 0 ]"

End Sub