【问题标题】:Check if object exists in VBScript检查VBScript中是否存在对象
【发布时间】:2014-02-11 14:48:01
【问题描述】:

我正在使用 HP ALM API 从数据库中获取字段。问题是它可能发生,该字段在项目中不存在,每次发生这种情况时,我都会收到以下错误。

如何正确检查字段对象以确保不再收到此“无效的自定义字段名称”?

代码:

Set field = custFields.Field("TEST", "TS_USER_88") <-- crashes here

label = field.UserLabel
If label = Null Then
    Print "[" & Sysdate() & "] Project can NOT be migrated..."
    Print "[" & Sysdate() & "] FIELD TS_USER_88 NOT FOUND - PROJECT IS NOT SUPPORTED."
Else
    ...
End If

错误:

xy.vbs(126, 7) (null): Invalid customization field name

【问题讨论】:

    标签: vbscript hp-quality-center hp-alm


    【解决方案1】:

    您需要将代码包装在“On Error Resume Next”中,然后处理错误。

    On Error Resume Next
    Set field = custFields.Field("TEST", "TS_USER_88")
    
    If Err.Number <> 0 Then
      'Do Something to handle your error
      'stuff
    
      'Clear the error
      Err.Clear
    End If
    On Error Goto 0
    
    'more stuff down here
    

    以下是有关 Err 对象及其一些属性的更多信息:

    http://msdn.microsoft.com/en-us/library/sbf5ze0e(v=vs.84).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-27
      • 2013-09-26
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多