English 中文(简体)
Microsoft VBA idiom (Visio) For Testing Non-Existence of a property?
原标题:

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
最佳回答

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.

问题回答

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.





相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签