VBA Excel 2010编辑器将属性名称大写,并且无法访问 [英] VBA Excel 2010 Editor capitalizes property name and it cannot be accessed
问题描述
作为对POST的响应,服务器返回非常简单的JSON对象,该对象可以指示上传成功或失败.
As a response to a POST, the server returns very simple JSON object that can indicate success or failure of the upload.
JSON对象可以是{config_version:N}表示成功,也可以是{exception:Reason}表示失败.
The JSON object can be either {config_version:N} to indicate success or {exception:Reason} to indicate failure.
我以以下方式处理响应:
I process the response in the following manner:
json = objHTTP.responseText
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
Set o = sc.Eval("(" + json + ")")
On Error GoTo Exception:
' try to use config_version property
Util.SetConfigData "version", o.config_version
Exit Function
Exception:
If o.Exception = "config_not_valid" Then
' one kind of exception - do accordingly
Else
' another kind of exception
End If
我面临的问题是VBA Excel 2010编辑器将o.exception
大写为o.Exception
!在Watch
窗口中,我看到属性exception
,但是无法访问它.如何强制编辑器停止使用大写字母的属性名称?还有其他语法可用于访问VBA中的属性吗?
The problem that I am facing is that the VBA Excel 2010 editor is capitalizing o.exception
to o.Exception
! In the Watch
window I see the property exception
but cannot access it. How can I force the Editor to stop capitalizing the property name? Is there other syntax for accessing properties in VBA?
编辑:为了更好地说明此处发生的情况,有两个屏幕截图.
EDIT: To better illustrate what is happening here are two screen shots.
如您所见,o
具有一个称为exception
的属性.尝试使用o.Exception
访问它时,将触发438错误.
As you can see, the o
has a property called exception
. When trying to access it with o.Exception
it triggers the 438 error.
由于我可以控制后端,因此我将响应更改为返回{Exception:whatever}
(无法永久执行).现在,VBA可以轻松选择该属性.
Since I have control over the backend, I changed the response to return {Exception:whatever}
(cannot do it permanently). Now VBA has no problem picking the property.
如果Jean-FrançoisCorbett建议VBA编译器不区分大小写,怎么办?
How is this possible if the VBA compiler is case-insensitive as suggested by Jean-François Corbett?
推荐答案
这很奇怪.我可以将小写的"e"保留在Tester()中,但是如果我将Test2()中的"exception"声明更改为"Exception",那么 VBE会自动更改Tester()中的任何实例)更改为例外" .改回例外"具有相反的效果.
This is wierd. I can get the lower-case "e" to stick in Tester(), but if I change the declaration of "exception" in Test2() to "Exception", then the VBE automatically changes changes any instances in Tester() to "Exception". Changing back to "exception" has the reverse effect.
基本上,您需要确保在项目中的其他地方都没有使用"Exception".
Basically you need to make sure you're not using "Exception" anywhere else in your project.
Sub Tester()
Dim sc, o
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
Set o = sc.Eval("({exception:'blahblah'})")
Debug.Print o.exception 'prints blahblah
End Sub
Sub Test2()
Dim exception As String
exception = "blah"
End Sub
如果仍然遇到问题,可以尝试这种方式,将已解析的JSON对象保留在脚本控件实例中:
If you're still stuck then you can try it this way, leaving your parsed JSON object in the script control instance:
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
sc.Eval "var o = eval({exception:'blahblah',message:'oops'})"
Debug.Print sc.Eval("o.exception") 'prints blahblah
Debug.Print sc.Eval("o['exception']") 'same thing...
Debug.Print sc.Eval("o.message") 'prints oops
这篇关于VBA Excel 2010编辑器将属性名称大写,并且无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!