缺少VBA编译器消息,错误的方法名称 [英] Missing VBA compiler message for wrong method name
问题描述
考虑以下代码:
Public Sub VBACompilerIsMad()
Dim Ap As Application
Dim Wb As Workbook
Dim Ws As Worksheet
Debug.Print Ap.XXX'无编译错误
Debug.Print Wb.XXX'无编译错误
Debug.Print Ws.XXX'编译错误
End Sub
当我编译这个,我得到一个编译器错误,用于引用 Worksheet
的不存在的成员。但是,如果我注释掉最后一行,就没有编译器错误,即使应用程序
或工作簿
都有方法或财产 XXX
。就好像我将 Ap
和 Wb
声明为 Object
变量
为什么编译器对待应用程序
/ 工作簿
不同于 Worksheet
?
有没有其他类似这样,编译器似乎看起来好像是对象
?
正如我已经解释),这是一个COM功能。
默认情况下,COM假定一个接口是可扩展的,也就是允许在运行时添加成员。如果不是所需的行为,可以使用 [不可扩展]
属性到接口定义,它声明接口只接受在类型库中显式定义的方法。
dispinterface _Application
和 dispinterface _Workbook
没有在
类似地,,ADO的 dispinterface _Connection
没有 [不可伸缩]
, dispinterface _Command
。
要了解哪些是可扩展的,请添加对 TypeLib Info
在项目的参考文献中运行:
Dim t As tli.TLIApplication
Set t = New tli.TLIApplication
Dim ti As t li.TypeLibInfo
设置ti = t.TypeLibInfoFromFile(excel.exe)
Dim i As tli.InterfaceInfo
对于每个i在ti.Interfaces中
如果(i.AttributeMask和tli.TYPEFLAG_FNONEXTENSIBLE)<> tli.TYPEFLAG_FNONEXTENSIBLE然后
Debug.Print i.Name
结束如果
下一个
你会看到几乎所有的接口在这里都是可扩展的,所以大部分的接口被推出调试窗口,你只能看到最后的接口。将<>
更改为 =
以打印不可扩展的那些,其中少得多。 / p>
Consider the following code:
Public Sub VBACompilerIsMad()
Dim Ap As Application
Dim Wb As Workbook
Dim Ws As Worksheet
Debug.Print Ap.XXX ' No compile error
Debug.Print Wb.XXX ' No compile error
Debug.Print Ws.XXX ' Compile error
End Sub
When I compile this, I get a compiler error for referring to an inexisting member of Worksheet
. However, if I comment out the last line, there is no compiler error, even though neither Application
nor Workbook
have a method or property XXX
. It is as if I declared Ap
and Wb
as Object
variables.
Why does the compiler treat Application
/ Workbook
differently from Worksheet
?
Are there any other classes like this, that the compiler seems to treat as if they were Object
?
As I have been explained (kudos go respectively), this is a COM feature.
By default COM assumes an interface is extensible, that is, it allows adding members at run time. If that is not the desired behaviour, one can apply the [nonextensible]
attribute to the interface definition, which declares the interface only accepts methods explicitly defined in the type library.
dispinterface _Application
and dispinterface _Workbook
do not have this flag set in the Excel type library, dispinterface _Worksheet
does.
Similarly, ADO's dispinterface _Connection
does not have [nonextensible]
, dispinterface _Command
does.
To learn which are extensible, add a reference to TypeLib Info
in the project's References and run:
Dim t As tli.TLIApplication
Set t = New tli.TLIApplication
Dim ti As tli.TypeLibInfo
Set ti = t.TypeLibInfoFromFile("excel.exe")
Dim i As tli.InterfaceInfo
For Each i In ti.Interfaces
If (i.AttributeMask And tli.TYPEFLAG_FNONEXTENSIBLE) <> tli.TYPEFLAG_FNONEXTENSIBLE Then
Debug.Print i.Name
End If
Next
You will see that almost all interfaces are extensible here, so most of them get pushed out of the debug window and you will only see the last ones. Change the <>
to =
to print those that are not extensible, there are much less of them.
这篇关于缺少VBA编译器消息,错误的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!