Sunday, 9 September 2012

Copying Excel data from one excel file to another file using QTP Automation

Step wise approach for copying excel data from one excel file to another(when the destination workbook is already created):-
  1. Create the excel object
  2. Open the source workbook(excel whose data need to be copied)
  3. Open the source worksheet(sheet whose data need to be copied)
  4. Now,open the excel workbook in which we need to copy the data(say destination workbook)
  5. Open the destination worksheet
  6. Copy the data from source workbook and paste into destination worksheet.
  7. Save the destination workbook.
  8. Set excel object to nothing to free the allocated memory to object.
Function CopyData(SourceExcel,DestnExcel,SourceSheetName,DestnSheetName)
    Set ExcelObj=CreateObject("Excel.Application")
    Set SourceWorkBook=ExcelObj.WorkBooks.Open(SourceExcel)
    Set SourceWorkSheet=SourceWorkBook.WorkSheets(SourceSheetName)
    Set DestinationWorkBook=ExcelObj.WorkBooks.Open(DestinationExcel)
    Set DestinationWorkSheet=SourceWorkBook.WorkSheets(DestinationSheetName)
    SourceWorkSheet.UsedRange.Copy
    DestinationWorkSheet.Range("").PasteSpecial Paste=xlValues
    DestinationWorkBook.Save
    DestinationWorkBook.Close
    SourceWorkBook.Close
    Set ExcelObj=Nothing  
End Function

In the  function ,I have assumed that the destination excel file is already created.    

No comments:

Post a Comment