VBA 返回“运行时错误 91:对象变量或块变量未设置”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13292364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:24:49  来源:igfitidea点击:

VBA returning "Run-time error 91: Object variable or With block variable not set"

vbaexcel-vbaexcel

提问by MartinUKPL

Ok, so I'm trying to make a complex geocoding script in VBA. I have written the following code and for some reason it returns an error ("Run-time error 91: Object variable or With block variable not set"). An example of a link that I use can be: "https://maps.googleapis.com/maps/api/geocode/xml?address=1+Infinite+Loop,+Cupertino,+Santa+Clara,+California+95014&sensor=false".

好的,所以我正在尝试在 VBA 中制作一个复杂的地理编码脚本。我编写了以下代码,由于某种原因,它返回一个错误(“运行时错误 91:对象变量或块变量未设置”)。我使用的链接示例可以是:“https://maps.googleapis.com/maps/api/geocode/xml?address=1+Infinite+Loop,+Cupertino,+Santa+Clara,+California+95014&sensor =假”。

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

Can anyone tell me what I'm doing wrong?

谁能告诉我我做错了什么?

回答by Tomalak

SelectSingleNode()may return Nothing.

SelectSingleNode()可能会回来Nothing

Never call a property (like .Text) on a function if the result of that function can be Nothing.

不要调用一个属性(比如.Text上的功能)如果函数的结果可以Nothing

Do something like this to avoid this error:

执行以下操作以避免此错误:

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

回答by Kevin Pope

This always happens to me when I try to assign a value to an object without using set. Try this:

当我尝试在不使用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

回答by Brad Thomas

Why the spaces before /location/lat, and .Text on lng but not lat?

为什么 /location/lat 之前的空格和 lng 上的 .Text 而不是 lat?