【发布时间】:2018-08-10 13:20:04
【问题描述】:
我是 VBA 的新手,还没有完全习惯语法,所以如果我的问题听起来很愚蠢,我很抱歉。
我在 Word 2010 中使用 RequisitePro40 和 VBA 7.0。在我的一个模块中,我有以下循环和 If 条件:
Dim rqRequirements As ReqPro40.Requirements
Dim rqRequirement As ReqPro40.Requirement
Const eAttrValueLookup_Label = 4
Dim a As Integer
...
For Each vReqKey In rqRequirements
Set rqRequirement = rqRequirements(vReqKey)
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text <> Null Then
a = 1
End If
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text = Null Then
a = 2
End If
Next
在循环的每次迭代中,a = 1 和 a = 2 都会被执行!!
基于This,等式和不等式运算符分别为“=”和“”。因此,我希望 a = 1 或 a = 2 对字符串执行。 我的语法有问题吗?还是与 ReqPro 相关的问题?
我也尝试使用“Is”和“IsNot”运算符,但它们会导致编译器错误:类型不匹配
有人可以帮我吗?
更新:实际目标是看看
rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text
是否为 Null。我添加了第二个 if 以表明该语句无法按我预期的方式运行。
将“Null”替换为“vbNullString”没有做出任何改变。
我还按照@Slai 的建议尝试了 IsNull 函数。结果几乎相同:
If IsNull(rqRequirement.AttrValue(att, eAttrValueLookup_Label).text) Then
a = 3
End If
If Not IsNull(rqRequirement.AttrValue(att, eAttrValueLookup_Label).text) Then
a = 4
End If
a = 3 和 a = 4 两个语句都为真且已执行。
【问题讨论】:
-
你是如何声明的?作为字符串?
-
错误在哪一行?
-
@QHarr 没有错误。但显然两个条件 text = Null 和 text Null 对于相同的文本都是正确的。这对我来说似乎不合逻辑,我不明白为什么会发生
-
1)
rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text返回的对象是什么类型的? (您可以查看 ReqPro40 对象模型文档) 2)您的实际目标是什么? (我无法理解您代码中的 If-Then 结构) -
使用 VBA,您通常不会测试字符串是否为“Null”。您将测试它是否为“空”,最有效的方法是:
Len(stringVariable) > 0如果strVariable = ""也是可能的,但执行效率不高。
标签: vba ms-word requirements