Search code examples
excelvbscriptqtphp-uft

If Err.Description = "...." Then ..... in VBscript


I'm fixing the bellow error "datatable importsheet operation failed. Invalid file" using this way:

On error resume next

DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2" 

If Err.Description = "The DataTable.ImportSheet operation failed. Invalid file." \n "Line (20): "DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2""."  Then

list of instructions

end if 

But I got this error:

The test run cannot continue due to a syntax error.

Expected 'Then'

Line (24): "If Err.Description = "The DataTable.ImportSheet operation failed. Invalid file." \n "Line (20): "DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2""."  Then".

Everything looks right. Please any help ? where I'm wrong ?


Solution

  • I'm really struggling with what you are trying to do and your comments are not helping...

    When using

    On Error Resume Next
    

    and you encounter an error while executing a statement the following things will happen;

    1. The statement will be skipped and the script will move to the next statement and carry on execution.
    2. The object Err will be populated with details of the error that caused the statement to be skipped.

    The Err object is made up of a few key properties that describe the error that occurred;

    • Number - The Error Code of the error that was raised.
    • Source - The Source of the error that was raised, this can be 3rd party library, the VBScript Runtime etc.
    • Description - A free text description of the error.

    If you want to check for error

    The DataTable.ImportSheet operation failed. Invalid file.

    when

    DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2"
    

    is executed you need to capture the Err.Number code that corresponds to that specific error.

    Something like this;

    On Error Resume Next
    
    DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2" 
    'Check that an error occurs
    If Err.Number <> 0 Then
      'Identify specific error and deal with accordingly.
      Select Case Err.Number
      Case 20012 'Code for DataTable.ImportSheet operation failed
        'List of instructions...
      Case Else
        'Unhandled error show to screen
        MsgBox "Error: " & Err.Number & " (" & Err.Source & ") - " & Err.Description
      End Select
      'Finished handling the error
      Call Err.Clear()
    End If
    

    What I find most confusing is the miss-information in this thread.

    • "I need the description and not the number and the problem is to use Err.Details and not Err.Description in my case. Now, it is working." - Ref

      The Err object does not contain a property called Details unless this is something specific to QTP but so far I haven't been able to find anything with a quick Google Search. So how you can say "it is now working" is beyond me.