2014-03-07 70 views
2

我正在使用以下函數,我通過Tools-> References添加了對Microsoft XML 6.0的引用。我不斷收到「編譯錯誤:用戶定義類型未定義」。任何人都可以看到這個缺陷嗎?調試器不斷突出顯示「googleResult As New MSXML2.DOMDocument」謝謝!Excel GoogleGeocode VB錯誤

Function GoogleGeocode(address As String) As String 
    Dim strAddress As String 
    Dim strQuery As String 
    Dim strLatitude As String 
    Dim strLongitude As String 

    strAddress = URLEncode(address) 

    'Assemble the query string 
    strQuery = "http://maps.googleapis.com/maps/api/geocode/xml?" 
    strQuery = strQuery & "address=" & strAddress 
    strQuery = strQuery & "&sensor=false" 

    'define XML and HTTP components 
    Dim googleResult As New MSXML2.DOMDocument 
    Dim googleService As New MSXML2.XMLHTTP 
    Dim oNodes As MSXML2.IXMLDOMNodeList 
    Dim oNode As MSXML2.IXMLDOMNode 

    'create HTTP request to query URL - make sure to have 
    'that last "False" there for synchronous operation 

    googleService.Open "GET", strQuery, False 
    googleService.send 
    googleResult.LoadXML (googleService.responseText) 

    Set oNodes = googleResult.getElementsByTagName("geometry") 

    If oNodes.Length = 1 Then 
    For Each oNode In oNodes 
     strLatitude = oNode.ChildNodes(0).ChildNodes(0).Text 
     strLongitude = oNode.ChildNodes(0).ChildNodes(1).Text 
     GoogleGeocode = strLatitude & "," & strLongitude 
    Next oNode 
    Else 
    GoogleGeocode = "Not Found (try again, you may have done too many too fast)" 
    End If 
End Function 


Public Function URLEncode(StringVal As String, Optional SpaceAsPlus As Boolean = False) As String 
    Dim StringLen As Long: StringLen = Len(StringVal) 

    If StringLen > 0 Then 
    ReDim result(StringLen) As String 
    Dim i As Long, CharCode As Integer 
    Dim Char As String, Space As String 

    If SpaceAsPlus Then Space = "+" Else Space = "%20" 

    For i = 1 To StringLen 
     Char = Mid$(StringVal, i, 1) 
     CharCode = Asc(Char) 

     Select Case CharCode 
     Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126 
     result(i) = Char 
     Case 32 
     result(i) = Space 
     Case 0 To 15 
     result(i) = "%0" & Hex(CharCode) 
     Case Else 
     result(i) = "%" & Hex(CharCode) 
     End Select 
    Next i 
    URLEncode = Join(result, "") 
    End If 
End Function 
+3

您是否向VBA項目添加了對MSXML的引用? –

+0

是的,Microsoft XML 6.0通過工具 - >參考。 –

+0

對於我來說這適用於該參考。檢查是否有任何其他引用標記爲「丟失」 –

回答

1

後期綁定解決了問題。

0

我有同樣的原始問題,並通過添加60這兩個DIM語句解決了這個問題:

Dim googleResult As New MSXML2.DOMDocument60 
Dim googleService As New MSXML2.XMLHTTP60 

即終止是由VBA建議,如果你立即開始重新鍵入該行,你到了「MSXML2 「。一部分。它現在可以在我的系統上正常工作。