Forum Discussion

DCat1223's avatar
DCat1223
Frequent Contributor
5 years ago
Solved

Write a variable value to Excel in Keyword Test

Hi.   I have a Keyword test I am looking for assistance with.  I am testing a process that creates a position that will later be filled by a newly hired employee.  In the create postion process, ...
  • LinoTadros's avatar
    5 years ago

    I noticed you mentioned that you are trying to write the value to a different sheet of the workbook in excel.

    That won't work.  The Workbook HAS to be closed when you access it from TestComplete due to the fact that Excel Workbooks REQUIRE exclusive locks.  Even if it is a different sheet, it won't work, the workbook has to be closed.

     

    If that is not a big deal, then the code below should get you going to write whatever value you want to Excel:

    function WriteDataToExcel()
    {
      let app = Sys.OleObject("Excel.Application");
      let book = app.Workbooks.Open("C:\\Users\\Lino\\Documents\\LinoTest.xlsx");
      let sheet = book.Sheets.Item("Sheet1");
      sheet.Cells.Item(1, 1).Value2 = "Lino" ;
      book.Save();
      app.Quit();
    } 

    This will write the value "Lino" to the A1 cell of the Sheet1 of the workbook.

     

    Hope that helps

    Cheers

    Lino