Monday, May 31, 2010

Overriding GetRowWithCellText Method

GetRowWithCellText method finds the specified text in a webtable and returns the number of the first row found.
If you want to find all matches in a webtable, then we have to override the GetRowWithCellText method using RegisterUserFunc as below,
RegisterUserFunc "WebTable","GetRowWithCellText","NewGetRowWithCellText"

Function NewGetRowWithCellText(obj,val1,val2,val3)
Dim v_st_returnText
Set objRow=obj.Object.getElementsByTagName("TR")
If val2<>0 Then val2=val2-1
If val3<>0 Then val3=val3-1
For i=val3 to objRow.Length-1
If val1=Trim(objRow(i).getElementsByTagName("TD")(val2).InnerText) Then
If v_st_returnText="" Then
v_st_returnText=Cstr( i+1)
v_st_returnText=Cstr( i+1)+","+v_st_returnText
End If
End If
End Function

Msgbox Browser("name:=QTP Codes").Page("title:=QTP Codes").WebTable("index:=0").GetRowWithCellText("Cell3",1,1)

Saturday, May 15, 2010

Quitting Excel Workbooks Using Getobject

Let’s consider that we have opened two excel workbooks using CreateObject. If you want to quit those two workbooks, try the below snippet.
Function fnc_CreateExcel
Set objNewExcel=CreateObject("Excel.Application")
Set objNewExcel=Nothing
End Function

Call fnc_CreateExcel'Creating Book1
Call fnc_CreateExcel'Creating Book2

Call fnc_Quit_Excel("Book1")'Quitting Book 1
Call fnc_Quit_Excel("Book2")'Quitting Book 2

Function fnc_Quit_Excel(v_St_ExcelName)
Set objExcel=GetObject(v_St_ExcelName).Application'Getting Excel Object using Book name
Set objExcel=Nothing
End Function