Microsoft VBA 习语 (Visio) 用于测试属性不存在?
我需要确保在 Visio 2003 上运行的宏不会在较低版本的 Visio 上引起问题:特别是因为我正在写入较低版本的 Visio 上不存在的属性。目前我正在这样做:
...
On Error GoTo NoComplexScriptFont:
Set cellObject = shapeObject.Cells("Char.ComplexScriptFont")
On Error GoTo ErrHandler
...
NoComplexScriptFont:
Rem MSGBOX only for debug
MsgBox "No Such Property"
GoTo endsub
ErrHandler:
Rem put in general error handling here
GoTo endsub
endsub:
End Sub
...
这有效,但我认为它有点混乱。我曾考虑过使用“Application.version”(对于 Visio 2003 返回“11”),但我想避免假设任何特定版本中可用的属性,而只是测试属性本身。
在 VBA 中执行此操作的正确习惯用法是什么?
谢谢
--- 下面有一些答案,我首选的解决方案是这个:
If shapeObject.CellExists("Char.ComplexScriptFont", 0) Then
msgbox "Property exists"
else
msgbox "Property does not exist"
end if
I need to ensure a Macro which works on Visio 2003 doesn't cause problems on lower versions of Visio: specifically because I'm writing to a property which doesn't exist on lower versions of Visio. Currently I'm doing this:
...
On Error GoTo NoComplexScriptFont:
Set cellObject = shapeObject.Cells("Char.ComplexScriptFont")
On Error GoTo ErrHandler
...
NoComplexScriptFont:
Rem MSGBOX only for debug
MsgBox "No Such Property"
GoTo endsub
ErrHandler:
Rem put in general error handling here
GoTo endsub
endsub:
End Sub
...
Which works, but its a little messy I think. I have toyed with the idea of using 'Application.version' (Which returns '11' for Visio 2003), but I would like to avoid assumptions about what properties are available in any particular release and just test for the property itself.
What's the nice proper idiom for doing this in VBA ?
Thanks
--- Got a few answers below, my preferred solution was this one:
If shapeObject.CellExists("Char.ComplexScriptFont", 0) Then
msgbox "Property exists"
else
msgbox "Property does not exist"
end if
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用包装函数来访问该属性,这样您就不会弄乱正常的错误处理,如下所示:(
注意:我只使用上面的
Object
因为我不知道什么typecellObject
等)即使对于我知道确实存在的属性,我也经常使用相同的技术,但这在某些情况下会引发错误。例如 (Excel),如果我要按名称访问工作表(如果不存在这样的工作表,则会引发错误),那么我将有一个调用
Worksheets(name)
并且返回一个Worksheet
对象或Nothing
:这使得调用代码更加清晰,因为您可以简单地测试返回值而不用担心错误处理。
I would use a wrapper function for accessing the property so that you don't mess up your normal error handling, like this:
(Note: I'm only using
Object
above because I don't know what typecellObject
etc. is)I often use the same technique even for properties that I know do exist, but which will raise an error under certain circumstances. For example (Excel), if I'm going to access a worksheet by name (which will raise an error if no such worksheet exists), then I'll have a wrapper function that calls
Worksheets(name)
and either returns aWorksheet
object orNothing
:This makes for much cleaner calling code, since you can simply test the return value rather than worrying about error handling.
您可以使用形状对象的 CellExists 属性来查看特定单元格是否存在。您必须传入您似乎已经在使用的 localeSpecificCellName,然后传入一个整数 fExistsLocally,它指定单元格的搜索范围;如果指定 0,则无论单元格是否继承,CellExists 将返回 true...如果指定为 1,则如果单元格继承,CellExists 将返回 false。
You can use the CellExists property of the shape object to see if a particular cell exists. You have to pass in the localeSpecificCellName, which you already seem to be using, and then you pass in an integer fExistsLocally, which specifies the scope of the search for the cell; if you specify 0 then the CellExists will return true if the cell is inherited or not...if it's 1 then CellExists will return false if the cell is inherited.