0

さて、VBAで複雑なジオコーディングスクリプトを作成しようとしています。次のコードを記述しましたが、何らかの理由でエラーが返されます(「ランタイムエラー91:オブジェクト変数またはブロック変数が設定されていません」)。私が使用するリンクの例は次のとおりです。"https://maps.googleapis.com/maps/api/geocode/xml?address=1+Infinite+Loop,+Cupertino,+Santa+Clara,+California+95014&sensor =false」。

Sub readXML(link As String)
Dim odc As DOMDocument
Dim lat As IXMLDOMElement
Dim lng As IXMLDOMElement

Set odc = New MSXML2.DOMDocument
odc.async = False
odc.Load (link)

lat = odc.SelectSingleNode("GeocodeResponse/result/geometry[location_type='ROOFTOP']/location/lat").Text
lng = odc.SelectSingleNode("GeocodeResponse/result/geometry[location_type='ROOFTOP']/location/lng").Text

Debug.Print lat & "; " & lng
End Sub

誰かが私が間違っていることを教えてもらえますか?

4

3 に答える 3

1

SelectSingleNode()戻る可能性がありNothingます。

.Text関数の結果がである可能性がある場合は、関数のプロパティ(など)を呼び出さないでくださいNothing

このエラーを回避するには、次のようにします。

Dim location As IXMLDOMElement
Dim locationPath As String

locationPath = "GeocodeResponse/result/geometry[location_type='ROOFTOP']/location"
Set location = odc.SelectSingleNode(locationPath)

lat = GetTextValue(location, "./lat")
lng = GetTextValue(location, "./lng")


' ------------------------------------------------------------------------

Function GetTextValue(node As IXMLDOMElement, Optional xpath As String = "") As String
  Dim selectedNode As IXMLDOMElement

  If xpath <> "" And Not node Is Nothing Then
    Set selectedNode = node.SelectSingleNode(xpath)
  Else
    Set selectedNode = node
  End If

  If selectedNode Is Nothing Then
    GetTextValue = ""
  Else
    GetTextValue = Trim(selectedNode.Text)
  End If
End Function
于 2012-11-08T16:06:40.687 に答える
0

/ location / latの前のスペース、およびlngの.Textで、latではないのはなぜですか?

于 2012-11-08T15:47:27.607 に答える
0

これは、を使用せずにオブジェクトに値を割り当てようとすると、常に発生しますset。これを試して:

Set lat = odc.SelectSingleNode("GeocodeResponse/result/geometry[location_type='ROOFTOP']/location/lat").Text
Set lng = odc.SelectSingleNode("GeocodeResponse/result/geometry[location_type='ROOFTOP']/location/lng").Text
于 2012-11-08T19:33:31.840 に答える