Tips on Working with FlexPro VBA
This topic provides some information on issues that might occur when programming in Visual Basic.
Using the Set Statement When Assigning the Value Property to Variables
Problems when Using OLE_COLOR Color Values
Deleting FlexPro Objects from a Collection within a For-Each Loop
Converting Numbers to Strings Using the CStr Function
Preventing Error Messages for Fully Automated Processes
Evaluation of Expressions in VBA (Note for C/C++ Developers)
Using the Set Statement When Assigning the Value Property to Variables
When assigning the Value property of a ValueObject object to a variable, you have to consider that the Value property is of the Variant type and can also return complex values, signals and lists in addition scalar values and arrays. Complex values, signals and lists are themselves objects in FlexPro and have to be assigned to variables using the Set statement.
The following statement only represents a valid assignment of a value if the value of oValueObject is neither complex nor a signal or list:
Dim vtValue As Variant
Dim oValueObject As ValueObject
...
vtValue = oValueObject.Value
If oValueObject.Value is complex or a signal or list, the error message 'Object does not support this property or method' appears. When assigning complex values, signals, or lists, you should therefore use the Set statement:
set vtValue = oValueObject.Value
When you are writing a macro and you do not know whether a value will be complex or real at runtime, using the IsObject statement can avoid errors when the macro is run:
If IsObject(oValueObject.Value) Then
Set vtValue = oValueObject.Value
Else
vtValue = oValueObject.Value
End If
If the value of oValueObject is an object, the Set statement is used; otherwise, the value has to be assigned using '='.
Problems when Using OLE_COLOR Color Values
The OLE_COLOR type is an OLE standard data type for storing RGB color values. In C/C++, the data type is declared as an unsigned Long value. VBA does not support unsigned values. Consequently, use of OLE_COLOR may occasionally cause problems. The following code might generate the error message 'Variable uses Automation type not supported in Visual Basic':
If ActiveDatabase.ActiveObject.Selection.FillFormat.Color <> _
fpColorRed Then
...
End If
There are two ways to avoid this error message. The first is to convert the color values to a Long value before the comparison:
If CLng(ActiveDatabase.ActiveObject.Selection.FillFormat.Color) <> _
fpColorRed Then
...
End If
The second is to introduce an OLE_COLOR variable, assign the color value to it, and then use the new variable for the comparison:
Dim clr As OLE_COLOR
clr = ActiveDatabase.ActiveObject.Selection.FillFormat.Color
If clr <> fpColorRed Then
...
End If
Deleting FlexPro Objects from a Collection within a For...Each Loop
Deleting FlexPro objects from a collection using a For...Each loop should be avoided, since it could cause unwanted results, and some elements in the collection might be missed.
If the elements of any collection of FlexPro objects are to be deleted, the following code leads to the desired result:
While oColl.Count > 0
oColl.Remove 1
Wend
or
While oColl.Count > 0
oColl(1).Delete
Wend
Converting Numbers to Strings Using the CStr Function
When using the CStr function to convert numbers to strings, you should consider that the function always uses the current Region and Language Options settings (Locale) for the conversion. On a German system, a ',' is used to separate the integral part of a decimal number from its fractional part, whereas the '.' is used on a British or American system. If you want to convert numbers independently of the system settings, you have to use the Windows API function SetLocaleInfo to change the system settings temporarily. In the A Custom Import Filter example you can see how this API function is used.
Preventing Error Messages for Fully Automated Processes
For fully automated processes that are to run without user interaction, you first have to make sure that all errors are handled by the macros themselves (consistent use of the On Error statement). Furthermore, make sure the following setting is enabled in the Visual Basic environment: under Tools > Options on the General tab, the setting for Error Trapping must be set to Break on Unhandled Errors. Otherwise, the execution of the program might be interrupted if allowed errors occur, and the error might be displayed, requiring user interaction (also see the DisplayAlerts property of the Application object).
Evaluation of Expressions in VBA (Note for C/C++ Developers)
As opposed to C/C++, where expressions are only evaluated from left to right for as long as necessary during condition evaluation, VBA always evaluates the complete expression.
The following C/C++ expression
(1) if(exp1 && exp2)
...
corresponds to the following in VBA code
(2) If exp1 And exp2 Then
...
If in (1) exp1 returns False, exp2 will no longer be evaluated and the code in the If statement will be executed immediately. In (2) exp2 is evaluated regardless of whether exp1 returns False or not. If exp1 must return False to validate exp2, the statement in VBA has to be disassembled:
If exp1 Then
...
If exp2 Then
...
End If
...
End If