Monday, 10 March 2014

QTP One Object to Another Object Model Combinations

------------------------------------------------------------------------------------------------------------ 'Export data from an Excel ... thumbnail 1 summary



------------------------------------------------------------------------------------------------------------
'Export data from an Excel file to a Text file
Dim objFso, objTextstream
Dim objExcel, objWorkbook, objWorksheet

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\abcd.txt", 2)

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open ("C:\Documents and Settings\Administrator\Desktop\dec1.xls")
Set objWorksheet = objWorkbook.Worksheets(1)

Rows_Count = objWorksheet.usedrange.rows.count

objTextstream.WriteLine "Agent  Password"
objTextstream.WriteLine "-------------------------"

For i = 2 to Rows_Count Step 1
objTextstream.WriteLine objWorksheet.Cells(i, 1)&", " & objWorksheet.Cells(i, 2)
Next
objTextstream.Close
objWorkbook.Close

objExcel.Quit
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing

Set objTextstream = Nothing
Set objFso = Nothing
--------------------------------------------------------------------------------
'Export data from a Text file to Excel file
Dim objFso, objTextstream
Dim objExcel, objWorkbook, objWorksheet

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\abcd.txt", 1)

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open ("C:\Documents and Settings\Administrator\Desktop\dec1.xls")
Set objWorksheet = objWorkbook.Worksheets(1)

i=1
Do Until objTextstream.AtEndOfStream
          myLine=objTextstream.ReadLine
          myField = Split (myLine,", ")
objWorksheet.Cells(i, 3) =myField(0)
objWorksheet.Cells(i, 4) =myField(1)
i=i+1
Loop

objTextstream.Close
objWorkbook.Save
objWorkbook.Close

objExcel.Quit
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing

Set objTextstream = Nothing

Set objFso = Nothing
----------------------------------------------------------------------------------------



No comments

Post a Comment