[python-win32] NEWBIE QUESTION - 'NoneType' object has no attribute'ActiveSheet'

Kenley _ fission6 at hotmail.com
Fri Nov 19 15:48:25 CET 2004


Wow. Thanks Mark!

Excel's startup process was the reason and the call to sleep allowed me to 
see what was really going on.

You were right, the  call to "Dispatch()" was spawning a new Excel process 
with no workbook, quicker than the eye could see. When clicking the button a 
second time, the call to Dispatch() does not spawn a new process for some 
reason and allows it to work correctly.

I got it to work with an embarrassingly crude hack (calling 'Dispatch' 
twice) that doesn't seem like it should be legal or work at all.

Here is the Button class code that works:

class ButtonEvent:
    def OnClick(self, button, cancel):
        #import time

        xlApp = Dispatch("Excel.Application")
        xlApp.Visible = 1
        del xlApp

        #seems to work ok with or without sleeping a few seconds
        #time.sleep(2)

        xlApp = Dispatch("Excel.Application")
        xlApp.Visible = 1
        xlApp.ActiveWorkbook.ActiveSheet.Cells(1).Rows.EntireRow.Delete(1)
        xlApp.ActiveWorkbook.ActiveSheet.Cells(2).Rows.EntireRow.Delete(1)
        xlApp.ActiveWorkbook.ActiveSheet.Cells(3).Rows.EntireRow.Delete(1)
        xlApp.ActiveWorkbook.SaveAs("C:\\test.csv")

        xlApp.ActiveWorkbook.Close(SaveChanges=0)
        xlApp.Quit()
        del xlApp


P.S. Mark your book rocks and I am part of the group that would buy a 
follow-up... Maybe you and Hugunin on Python with .Net?




More information about the Python-win32 mailing list