[python-win32] Showing/Selecting Excel sheets

Colin Campbell ccampbell@ede.org
Wed, 03 Jul 2002 19:05:07 -0600


Hello all,

After bashing this around, trawling the docs and getting greyer by the 
minute, I'm turning to the repository of wisdom that is this group.

With luck, I've buttered you up to the point you can handle yet another 
iteration of the vanishing Excel worksheet question. I'm sure I've seen it 
answered a month or so ago; blessed if I can find it!

I'm trying to open a workbook in Excel, using ActiveState Python 2.2.1, 
Excel 2002, and Windows 2000. Eventually, when I teach myself enough 
win32com, my code will read sheet numbers from a file, create a new 
workbook, copy the selected sheets into it, and show the result to the 
user. What is happening at this point in my admittedly incremental 
development, is that Excel fires up and opens the proper workbook, shows me 
only the menu, tool and formula bars, and refuses to select anything other 
then the first sheet in the book. If there is some fairly newby-ish 
documentation I'm missing, I'd be glad of a reference. My code is below.

Many TIA

Colin

#=============================================
#Excel sheet Copier/Hider
# trying to hardwire test values

from win32com.client import Dispatch
xl = Dispatch( 'Excel.Application' )    # create instance of Excel
xl.Visible = True                       # force it to be visible
xlFile = "C:/Other/Prg/Finstmts.xls"    # workbook to open
wkb = xl.Workbooks.Open(xlFile)         # tell excel to open it (ok to here)
wkb.Sheets(2).Select                    # show me sheet 2
z = raw_input()                         # pause the script
wkb.Close()                             # tidy up
xl.Quit()                               # shut down the app
xl = None                               # call the destructor

#========================================


--
I believe that the first test of a truly great man is his humility. I do 
not mean by humility, doubt of his own powers. But really great men have a 
curious feeling that the greatness is not in them, but through them. And 
they see something divine in every other man and are endlessly, foolishly, 
incredibly merciful.
  -John Ruskin, author, art critic, and social reformer (1819-1900)