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’.
The workflow for the process would be:
- Create an excel object.
- Create a workbook object that takes file path as input.
- Create a sheet name object that takes sheet name as input.
- Perform operations on sheet.
- …………………………………
- …………………………………
- …………………………………
- Quit excel application.
- 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.
1: Function ParamValues(InputFilePath,SheetName)
2: Dim input()
3: Set appExcel = CreateObject(“Excel.Application”) ‘Step 1
4: Set objWorkBook = appExcel.Workbooks.Open (InputFilePath) ‘Step 2
5: Set objSheet = appExcel.Sheets(SheetName) ‘Step 3
6: For i=2 to 50 ‘Step 4
7: if objSheet.cells(i,1).value <> “” then
8: Input(i-2)=Trim(objSheet.cells(i,1).value)
9: else
10: Exit For
11: End If
14: Next
15: appExcel.quit ‘Step 5
16: Set appExcel=Nothing ‘Step 6
17: Set objSheet=Nothing
18: Set objWorkBook=Nothing
19: 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.
Have you downloaded the FREE Optimizing QTP eBook yet? Get It Now!
I'm sure you have already subscribed to our feed but just in case not. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.



20 comments ↓
interesting…
But to Create or Manage external Data, People still stugle.
Here is a link which might give you some more idea of Test Data management: http://www.grid-tools.com/demos/Managing_Capture_replay_data.htm
http://www.grid-tools.com/datamaker-demo.php
Hi ,
This concept has really helped me in increasing the performance of my scripts as the time used to load the data in the script is reduced,
Thanks..
Hello,
I’ve a question in regards to the above piece of code. What if I use UsedRange function to get all the rows with some data?
In that case, how can I use the for loop to go through the excel sheet?
Also how can I use an array to collect data when looping through required rows in the external excel sheet (not the datatable).
Thanks in advance for all the help.
[...] software components –under Windows- to interact with each other. QTP’s Automation Object Model, Excel automation model comes under the broad heading of [...]
Hi,
Instead of importing the sheet, the data is loaded into a variable INPPUT , so you are using the lotof memory. otherway, create an ADODB object and then read the data using SQL as required for the AUT’s page.
Hi Ankur,
I have a problem running the code. Am getting an error in step2 stating “Excel cannot access “My Documents”.The document may be read-only or encrypted.” I even tried to change the location of the test input excel but same error..the test input file is not encrypted as well.
Let me know what can be possible cause of this error.
Thanks,
Kashif
Hi Ankur,
Can you please give information on how to connect to a SQL/Oracle database using QTP?
I am facing a problem related to this. I have QTP installed on CITRIX and every time i open it, it gets opened in a different server. now i am using TOAD application to connect to my database, but this application is installed on the different server than QTP. Is it possible to connect to database server (on a different server) using QTP?
Your help in this regard is highly appreciable. Thanks!!!
Hi,
I am doing some POC for automation on a .NET + WPF application. Though I am able to see the object’s properties when I am spying on the screen but not able to perform following:
1. When I am trying to record some basic actions on a screen, no recording is happening.
2. When I try to learn the objects from Object Repository Manager, it shows as if it is learning the objects and shows the message “Analyzing objects…” but no object is added to the repository.
Please suggest some resolution.
Thanks
Anuj
can we associate multiple test data files with single QTP script?
please advice !
hi guys,
can you please tell me how to read the 3rd row and 4th column data from xl sheet without using data table?
Thanks & regards
Rasheed
Is it possible to access the QTP Datatable in vb.net?
hi ankur,
i am working with qtp ver.10
can you please tell me how to capture objects from an .asp page.
and is there a .asp add in for qtp????
thanks and regards
how canwe handle erros other than using recovery scenerios in qtp
Does QTP 9.2 supports “Excel Automation Model”?
I tried this on v9.2 but could not get correct result. Please suggest.
I can import excel sheet in qtp scripts but not able to read multiple records from excelsheet in qtp script.
Please help me in doing all that task ….
Please check
this line
if objSheet.cells(i,1).value “” then
should be
if appExcel.cells(i,1).value “” then
Hi All, in this time i need your support.
here is my question.
clicking an image(which generated a excel sheet)-> i want to open the file -> check the excel sheet info with the data table info.
how i should write scripts for this functionality..?
Thanks,
Saranya
Hi, If we don’t import the Sheet, how do we get that sheet in the Test Result?
Hi Kashif,
I too had same problem, bur i solved it.. here is procedure
Create a new folder in C: for eg c:\test -> copy your xlsx file in this folder right click on folder open properties, uncheck readonly, and in security settings, give full control to group named “users”..Now it will work as it worked for me.
This is a noraml problem when you are not logged in with administrator privilages.
Cheers
Saurabh
how i can print values in the array
Leave a Comment