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

Kenley _ fission6 at hotmail.com
Wed Nov 17 16:12:07 CET 2004


I'm learning Python and experimenting with the demo COM addin for Excel 
(excelAddin.py) that is included with win32com extension.

I used makepy.py on the Excel Object Library and saved the file as Excel.py.

The only test modification I made to the demo code was to the ButtonEvent 
class. Here is the ButtonEvent class I'm testing:

class ButtonEvent:
    def OnClick(self, button, cancel):
        import win32ui # Possible, but not necessary, to use a Pythonwin GUI
        import win32con
        import Excel as xlApp

        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

All this is supposed to do is delete a few lines in whatever Excel document 
is currently open and save it as "test.csv".

THE PROBLEM: What is puzzling is that everything works great ONLY IF the 
button is clicked twice. If the button is clicked only once, excel just 
flashes. After clicking the button a second time everything works great.

I used the trace collector and this is what is shows the first time the 
button is clicked:

Object with win32trace dispatcher created (object=None)
OnConnection Microsoft Excel 1 A Simple Excel Addin (1,)
OnAddInsUpdate None
OnStartupComplete None
Object with win32trace dispatcher created (object=None)
OnConnection Microsoft Excel 1 A Simple Excel Addin (3,)
OnAddInsUpdate None
OnStartupComplete None
pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "D:\Python23\Lib\site-packages\win32com\server\policy.py", line 283, 
in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "D:\Python23\Lib\site-packages\win32com\server\policy.py", line 288, 
in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "D:\Python23\Lib\site-packages\win32com\server\policy.py", line 581, 
in _invokeex_
    return func(*args)
  File "D:\Python23\Lib\site-packages\Pythonwin\excelAddin_test.py", line 
71, in OnClick
    xlApp.ActiveWorkbook.ActiveSheet.Cells(1).Rows.EntireRow.Delete(1)
exceptions.AttributeError: 'NoneType' object has no attribute 'ActiveSheet'
OnBeginShutdown None
OnDisconnection

================================================

Is there something I'm missing in the class? What do I need to do to make 
the button do it's work on first click?

Thanks in advance for any help,
Ken (Enjoying Learning Python on Win32)




More information about the Python-win32 mailing list