Message 1 of 3
"On error resume next" vs. "for each"-loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
First of all, there is not really a problem to solve. It's more a question of what is best practice.
I wrote the below function to check for a specific parameter and add it if it's missing.
At first i use the for each loop to find the parameters (now commented out).
But i later realized i could just assign the parameters, while suppressing errors. Because later in the routine i check if the parameters exist anyway.
My question is, what would be the best practice in this case? The loop or the error suppressing? And why?
Sub TestThicknessInch() If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then Exit Sub Dim oPart As PartDocument Set oPart = ThisApplication.ActiveDocument Call AddThicknessInch(oPart.ComponentDefinition.Parameters) End Sub Private Function AddThicknessInch(ByRef oParas As Parameters) Dim Thickness As Parameter Dim ThicknessInch As Parameter ' Dim oPara As Parameter ' For Each oPara In oParas ' If oPara.Name = "Thickness_Inch" Then Set ThicknessInch = oPara ' If oPara.Name = "Thickness" Then Set Thickness = oPara ' Next oPara On Error Resume Next Set Thickness = oParas.Item("Thickness") Set ThicknessInch = oParas.Item("Thickness_Inch") On Error GoTo 0 If Thickness Is Nothing Then Exit Function If ThicknessInch Is Nothing Then Call oParas.UserParameters.AddByExpression("Thickness_Inch", "Thickness", "ft") Set ThicknessInch = oParas.Item("Thickness_Inch") End If If ThicknessInch.Units <> "ft" Then ThicknessInch.Units = "ft" If ThicknessInch.DisplayFormat <> kDecimalDisplayFormat Then _ ThicknessInch.DisplayFormat = kFractionalDisplayFormat If ThicknessInch.ExposedAsProperty = False Then _ ThicknessInch.ExposedAsProperty = True If Not ThicknessInch.CustomPropertyFormat.Precision = kSixtyFourthsFractionalLengthPrecision Then _ ThicknessInch.CustomPropertyFormat.Precision = kSixtyFourthsFractionalLengthPrecision End Function