Microsoft VBA 习语 (Visio) 用于测试属性不存在?

发布于 2024-08-10 07:16:11 字数 803 浏览 3 评论 0原文

我需要确保在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-08-17 07:16:11

我会使用包装函数来访问该属性,这样您就不会弄乱正常的错误处理,如下所示:(

...
Set cellObject = GetCellObject(shapeObject)
If Not cellObject Is Nothing Then
   ' Do something with cellObject
End If
...

Private Function GetCellObject(ByVal shapeObject As Object) As Object
    On Error Resume Next
    Set GetCellObject = shapeObject.Cells("Char.ComplexScriptFont")
End Function

注意:我只使用上面的 Object 因为我不知道什么type cellObject 等)

即使对于我知道确实存在的属性,我也经常使用相同的技术,但这在某些情况下会引发错误。例如 (Excel),如果我要按名称访问工作表(如果不存在这样的工作表,则会引发错误),那么我将有一个调用 Worksheets(name) 并且返回一个 Worksheet 对象或 Nothing

Private Function GetWorksheet(ByVal strName as String) As Worksheet
    On Error Resume Next
    Set GetWorksheet = Worksheets(strName)
End Function

这使得调用代码更加清晰,因为您可以简单地测试返回值而不用担心错误处理。

I would use a wrapper function for accessing the property so that you don't mess up your normal error handling, like this:

...
Set cellObject = GetCellObject(shapeObject)
If Not cellObject Is Nothing Then
   ' Do something with cellObject
End If
...

Private Function GetCellObject(ByVal shapeObject As Object) As Object
    On Error Resume Next
    Set GetCellObject = shapeObject.Cells("Char.ComplexScriptFont")
End Function

(Note: I'm only using Object above because I don't know what type cellObject 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 a Worksheet object or Nothing:

Private Function GetWorksheet(ByVal strName as String) As Worksheet
    On Error Resume Next
    Set GetWorksheet = Worksheets(strName)
End Function

This makes for much cleaner calling code, since you can simply test the return value rather than worrying about error handling.

烦人精 2024-08-17 07:16:11

您可以使用形状对象的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文