In some of our earlier posts we have seen How to work on QTP datatable, How to import/export datatable, what are the different functions available for datatable and also the basics of QTP Automation Object Model(AOM).
Here we will combine AOM and Excel automation model to import data from an external excel file to QTP without actually importing the file as a ‘datatable’ and use test data directly from external sheet.

The workflow for the process would be:

  1. Create an excel object.
  2. Create a workbook object that takes file path as input.
  3. Create a sheet name object that takes sheet name as input.
  4. Perform operations on sheet.
    1. …………………………………
    2. …………………………………
    3. …………………………………
  5. Quit excel application.
  6. Destroy objects defined above.

We will show the whole process with an example. We will create a function that will accept file path and sheet name as input and will give the output an array which will contain all the values of column 1 of the input sheet.

Function ParamValues(InputFilePath,SheetName)

Dim input()
Set appExcel = CreateObject("Excel.Application") 'Step 1
Set objWorkBook = appExcel.Workbooks.Open (InputFilePath) 'Step 2
Set objSheet = appExcel.Sheets(SheetName) 'Step 3

For i= 2 to 50 'Step 4
if objSheet.cells(i,1).value <> "" then
Input(i-2)=Trim(objSheet.cells(i,1).value)
else
Exit For
End If
Next

appExcel.quit 'Step 5
Set appExcel=Nothing 'Step 6
Set objSheet=Nothing
Set objWorkBook=Nothing

End Function

InputFilePath and SheetName are the input arguments passed to the function named ParamValue. The output of the above function will be an Input() array that will contain all the values of column 1 of the given excel sheet.
You can call the above function by: ParamValues <Your File Path>, <Sheet Name>

Copy the above code and test it by yourself to have more clarity.