VBA:运行时错误“91"? [英] VBA: Run time error '91'?
问题描述
我在这里要做的就是保存对当前活动窗口的引用,但它似乎不起作用.它在最后一行给了我一个运行时错误.
All I'm trying to do here is save a reference to the currently active window, but it doesn't seem to be working. It gives me a run time error on the last line.
Dim SourceWindow As Window, QACheckWindow As Window
SourceWindow = ActiveWindow
我不确定为什么.ActiveWindow
不是应该返回当前活动的窗口吗?如果没有,我如何引用它?
I'm not exactly sure why. Isn't ActiveWindow
supposed to return the currently active window? If not, how can I make a reference to it?
上面是我函数的开头,所以在它之前是 Sub FuncName()
The above is right at the beginning of my function, so all there is before it is Sub FuncName()
推荐答案
在 VB 中的对象变量需要指定 Set
关键字.作为对象的对象属性也需要Set
.当赋值不使用该关键字时,会引发运行时错误 91未设置对象变量".
In VB object variables require the Set
keyword to be assigned. Object properties that are objects also need to be Set
. Runtime error 91 "object variable not set" is raised when the assignment doesn't use that keyword.
继承自遗留的Let
关键字来分配值,以及Set
关键字来分配引用;Let
最终被弃用(尽管仍然需要定义属性)而 Set
仍然存在,保留了 VB6/VBA 赋值语法,如 [Let] variable = value
,其中Let"是可选的.
This is inherited from legacy Let
keyword to assign values, and Set
keyword to assign references; the Let
eventually was deprecated (although still needed for defining properties) and the Set
remained, leaving the VB6/VBA value assignment syntax like [Let] variable = value
, where "Let" is optional.
在声明和赋值中:
Dim SourceWindow As Window, QACheckWindow As Window
'this is like saying "Let SourceWindow = ActiveWindow":
SourceWindow = ActiveWindow
SourceWindow
是一个对象,被分配为一个值 - 这会导致 VBA 尝试 let-coercionem> 通过默认成员调用.如果对象未初始化,则成员调用失败并返回错误 91.如果对象已初始化但没有默认成员,则会引发错误 438.
SourceWindow
is an object, assigned as if it were a value - this causes VBA to attempt let-coercion through a default member call. If the object wasn't initialized, the member call fails with error 91. If the object was initialized but doesn't have a default member, error 438 is raised.
因此,在这种情况下,由于隐式成员调用而引发错误 91;.net 等效项将是 NullReferenceException
:
So in this case error 91 is being raised because of an implicit member call; the .net equivalent would be a NullReferenceException
:
Dim SourceWindow As Window, Dim WindowTitle As String
'"SourceWindow" reference isn't set, the object can't be accessed yet:
WindowTitle = SourceWindow.Caption
<小时>
我在这里有点过火了,但是遗留的 Let
statement 不应该与 Let
混淆子句(在 VB.net 中),它在 LINQ 查询语法(在 VB.net 中)计算一个值并将其分配给一个新的、查询范围的变量(示例取自MSDN):
I'm going to go a bit overboard here, but the legacy Let
statement should not be confused with the Let
clause (in VB.net) which, in the LINQ query syntax (in VB.net), computes a value and assigns it to a new, query-scoped variable (example taken from MSDN):
From p In products
Let Discount = p.UnitPrice*0.1 '"Discount" is only available within the query!
Where Discount >= 50
Select p.ProductName, p.UnitPrice, Discount
VB.net 分配 values 和 references,无需指定 Let
或 Set
,因为在 .net 中,这种区别是一条更细的线,考虑到一切最终是如何从 System.Object
... 包括 System.ValueType
派生的.这就是为什么 Set
关键字也在VB.net,以及为什么用于定义属性的 VB.net 语法放弃了 Let
以支持 Set
- 因为无参数默认成员 在 VB.NET 中是非法的,所以这种模棱两可的 let-coercion 不会发生.
VB.net assigns both values and references, without the need to specify a Let
or a Set
, because in .net this distinction is a much thinner line, given how everything ultimately derives from System.Object
... including System.ValueType
. That's why the Set
keyword was also deprecated in VB.net, and also why the VB.net syntax for defining properties has dropped the Let
in favor of Set
- because parameterless default members are illegal in VB.NET, so this ambiguous let-coercion doesn't happen.
这篇关于VBA:运行时错误“91"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!