1

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.

Laurens Ruben
  • 109
  • 5
  • 22

2 Answers2

0

Your approach with On Error Goto <label> is correct. But you will want to reset the error handler so it will not jump to the label upon another error. You could use:

    On Error GoTo label1
    Set ParamV = partDoc.Part.Parameters.Item("ParName")
    On Error GoTo 0        ' reset the error handler upon success
    Dostuffwith ParamV
    GoTo label 2
label1:
    On Error GoTo 0        ' reset the error handler after an error
label2:

An alternative is to use the resume method of error handling:

    On Error Resume Next
    Set ParamV = partDoc.Part.Parameters.Item("ParName")
    MyErrNumber = Err.Number
    On Error GoTo 0   ' Reset error handling
    If MyErrNumber <> 0 Then GoTo label1
    Dostuffwith ParamV

label1:
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

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 
Quima
  • 894
  • 11
  • 21