Forum Discussion

forrestaustin's avatar
6 years ago

Having Trouble switching to different sheet in excel using sys.oleobject

I was able to use python inside testcomplete to open an excel workbook I have and read some values. Now I am trying to change sheets from the default "Sheet1" to "Sheet2"


My code is:



def ReadDataFromExcel():

  Excel = Sys.OleObjext["Excel.Application"]

  book = Excel.Workbooks.Open("path to excel sheet")

  sheet2 = book.Sheets("Sheet2")

.... At this point the code breaks and gives me the error "RuntimeError: The specific object is not callable"


Now if I debug and put a break point right above last step and look at the local variables. The book object is not empty and it recognizes that it has 3 sheets.


Also if I follow the sample from the documentation using "ActiveSheet" the program runs fine.


Does anyone have any ideas on what could be wrong?

  • tristaanogre's avatar
    6 years ago

    Yes, I did... which is why I suggested a change... change from () to [].

8 Replies

  • shankar_r's avatar
    Community Hero

    try this line instead of your, it should work.

      sheet2 = book.Sheets.Item("Sheet2")

    • forrestaustin's avatar

      When I try that suggestion I get the following error: Python runtime error


      TypeError: 'IdispatchIndexedPropertyWrapper' object is not callable


      Do you know how I can fix this?

  • Marsha_R's avatar
    Champion Level 3

    Do you actually have a typo in this line?

    Excel = Sys.OleObjext["Excel.Application"]


    (should be OleObject)

    • forrestaustin's avatar

      Hi Marsha, thanks for catching that. I actually had OleObject on my Testcomplete but it is on an offline computer so there was a mistake when  I was manually typing the code here.