I'm trying to check for the existence of specific parameters in a part and if it does not exist then I want to skip a small section of my code. This is my current code that works as desired:
Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument
Dim ParamV As Parameter
Set ParamV = partDoc.Part.Parameters.Item("ParName")
Dostuffwith ParamV
Now I want to add a check before executing the last 2 lines of code, it would become something like this:
Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument
Dim ParamV As Parameter
If partDoc.Part.Parameters.Item("ParName") Exists
then
Set ParamV = partDoc.Part.Parameters.Item("ParName")
Dostuffwith ParamV
End If
I tried using
On Error goto label1
Set ParamV = partDoc.Part.Parameters.Item("ParName")
Dostuffwith ParamV
label1
But this is impossible because On Error needs to end with a Resume or Resume Next. I couldn't find a way to make it Resume after "Dostuffwith ParamV", it will always Resume at the line of code that prompted the error in the first place.
I also tried
If not partDoc.Part.Parameters.Item("ParName") is nothing
Then
Set ParamV = partDoc.Part.Parameters.Item("ParName")
Dostuffwith ParamV
End If
But this also gives an error because parameter ParName just doesn't exist.
I don't know what else I can try, please help.
You can uso the On Error Resume Next
clause, and then check for the Err.Number
to see if any errors occurred:
On Error Resume Next
Err.Clear 'Clear any previous error messages
Set ParamV = partDoc.Part.Parameters.Item("ParName")
if Err.Number = 0 then
'TODO Stuff if Parameter Exists
else
'TODO Stuff if parameter does not Exist
end if
Also, you could create a function to test and return the Parameter.
Public Function ParameterExist(byref ParameterCollection as Parameters, byval ParameterName as string, Byref OutputParameter as Parameter) as Boolean
On Error Resume Next
Err.Clear
Set OutputParameter = ParameterCollection.Item(ParameterName)
if Err.Number = 0 then
ParameterExist = True
else
ParameterExist = False
end if
end function
Usage:
dim Param as Parameter
If ParameterExist(PartDoc.Part.Parameters, "ParName", Param) then
'Param will hold the parameter object
end if