Ask a Question

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

SOLVED
forrestaustin
Contributor

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?

1 ACCEPTED SOLUTION

Accepted Solutions
tristaanogre
Community Hero

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

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


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

8 REPLIES 8
Marsha_R
Community Hero

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

Do you actually have a typo in this line?

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

 

(should be OleObject)

Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
shankar_r
Community Hero

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

try this line instead of your, it should work.

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


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

forrestaustin
Contributor

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

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?

forrestaustin
Contributor

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

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. 

tristaanogre
Community Hero

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

Try

 

sheet2 = book.Sheets.Item["Sheet2"]


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
forrestaustin
Contributor

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

Hi Tristan, did you see my error I get when I try that?

tristaanogre
Community Hero

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

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


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

forrestaustin
Contributor

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

Oh sorry I couldn't discern that change, but it doesn't throw that error any more after changing to []. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: