How do you handle runtime error in QTP when specified excel sheet specified in script does not exist

Questions by jayamalekati

Showing Answers 1 - 3 of 3 Answers

AdvancedQTP

  • Nov 13th, 2007
 

You can wrap your code with error-handling code. This example pops an error message and exits the sub. You can change the code to fit your needs:

Sub LoadExcel(sExcelFile, sSheetName)

   On Error Resume Next
   Set oExcel = CreateObject("Excel.Application")

   'What if Excel isn't installed
   If err.Number <> 0 Then
      MsgBox "Can't Initiate excel." & vbCrLf & _
      "Make sure MS excel to be installed.", vbCritical
      Exit Sub
   End If

   oExcel.Workbooks.Open(sExcelFile)

   'What if Excel file doesn't exist?              
   If err.Number <> 0 Then
      MsgBox "Can't load excel file = " & sExcelFile, vbCritical
      Exit Sub
   End If

   Set oSheet = oExcel.Worksheets(sSheetName).UsedRange

    'What if the sheet doesn't exist?              
   If err.Number <> 0 Then
      MsgBox "Can't load sheet = " & sSheetName, vbCritical
      Exit Sub
   End If

   'Continue excel data-load

End Sub

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions