Micro Focus QTP (UFT) Forums
Run time error - Excel object - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Run time error - Excel object (/Thread-Run-time-error-Excel-object)



Run time error - Excel object - sasi1982 - 11-03-2010

Hi,
I have a simple code where it creates excel object and add data to it. But i am getting the below run time error when i execute it. Can some please check and let me know solution.

run time error : ActiveX component can't create object: 'Excel.Application'

Thanks in advance.


RE: Run time error - Excel object - KavitaPriyaCR - 11-03-2010

Is statement like:
CreateObject("Excel.Application")


RE: Run time error - Excel object - sasi1982 - 11-03-2010

Yes kavitha. With that statement only i am getting the runtime error.



Kavitha, if i run the piece of code individually from QTP, it is working fine.

But, if add the funtion definition to the .vbs file and then call it from QTP, it is not working. .vbs is associated to the script. Can you please reply back immediately if you have solution.

Thanks ..


RE: Run time error - Excel object - A.Saini - 11-03-2010

HI Sasi,

I think you should double check whether the function is properly called from the Script.

You can use the "Alt+G" to check that.

Smile


RE: Run time error - Excel object - sasi1982 - 11-03-2010

Hi saini,
i have checked it. it is fine.


RE: Run time error - Excel object - KavitaPriyaCR - 11-03-2010

Hi Sasi
please check my code, i tried to simulate your error.
1. Function definition:
Code:
Public Function ReadExcel()
Set appExcel = CreateObject("Excel.Application")
Set objWorkBook = appExcel.Workbooks.Open ("C:\Documents and Settings\Administrator\Desktop\ActualizedTrades_06Mar10.xls")     'opens the sheet
Set objSheet = appExcel.Sheets("ACTUALIZED_TRADE")             ' To select particular sheet
Column_Count=objSheet.usedrange.columns.count
Row_Count=objSheet.usedrange.rows.count
Environment.value("data1")=objSheet.cells(1.1).value
End Function

2. Associate .vbs file in settings >> resources

3. Main script in QTP:
Code:
Call ReadExcel()
MsgBox Environment.Value("data1")
' This line will print the cell data

This is working fine for me. can you please check, is it the way you are trying to do? OR am i understanding it other way?


RE: Run time error - Excel object - sasi1982 - 11-03-2010

Kavitha,
Thank you so much for the reply. Here probably, the problem is , when i run the script in the invisible mode, then it is throwing this error. But if i run the script from QTP UI, it is not working. I am copying the code here for your reference. Please have a look.



Code:
public Function Export_Result(TestID,Driver_Script,Res,Comments)

              Dim objExcel,objBook,objSheet,i,columnCount,rowCount
            ' Initializing the Excel sheet
            Set objExcel = CreateObject("Excel.Application")
            Set objBook = objExcel.Workbooks.Open("Q:\Automation\Reports\Results.xls")
            Set objSheet=objBook.Worksheets("Test_Results")
            'Getting the TimeStamp
            ResultTime=now
            ' Getting the used rows range
            rowCount=objSheet.usedrange.rows.count
            ' Writing in Excel Sheet
            objExcel.Worksheets("Test_Results").Cells(rowCount+1, 2).Value = TestID
            objExcel.Worksheets("Test_Results").Cells(rowCount+1, 3).Value = Driver_Script
            objExcel.Worksheets("Test_Results").Cells(rowCount+1, 4).Value = Res
            objExcel.Worksheets("Test_Results").Cells(rowCount+1, 4).Font.Bold = true
            If ucase(Res) = "PASS" Then
                    objExcel.Worksheets("Test_Results").Cells(rowCount+1, 4).Font.ColorIndex  = 10
                    else
                    objExcel.Worksheets("Test_Results").Cells(rowCount+1, 4).Font.ColorIndex  = 3
            End If
            
            objExcel.Worksheets("Test_Results").Cells(rowCount+1, 6).Value = ResultTime
                        
            'Writing the Comments
           objExcel.Worksheets("Test_Results").Cells(rowCount+1, 5).Value = Comments
            Export_Result = true
            ' Saving and closing the work book
            objBook.Save
            objExcel.Workbooks.Close
            objExcel.Quit

End Function

Thanks
Sasi.C


RE: Run time error - Excel object - KavitaPriyaCR - 11-03-2010

Hi sasi
I tried your code it self.It is working fine for bothe the cases mentioned below:
1. When i run the script from QTP UI.
2. when i run the script in the invisible mode (after replacing the parameters with data).