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

Tim Roberts timr at probo.com
Wed Nov 17 23:27:28 CET 2004


On Wed, 17 Nov 2004 09:12:07 -0600, "Kenley _" <fission6 at hotmail.com> wrote:

>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
>  
>

Several problems with this.

First, with few exceptions, "import" statements should always be at the 
top of your file.  "import" is an executable statement, so with your 
code, the modules will get reimported every time you click the button.  
However, you aren't using anything in ANY of those three modules, so 
just delete the imports.

Next, you import the Excel module as xlApp, and then promptly overwrite 
it with the results of Dispatch, which deletes the module you just 
imported.  Consider that your code is basically identical to:

    import Excel
    xlApp = Excel
    del Excel
    xlApp = Dispatch("Excel.Application")

Generally, you shouldn't have to manipulate the results of makepy.py.  
It stores its result in a cache within the Python site-packages area.  
win32com.client.Dispatch will look in that cache for a match when you 
create an object, and return the appropriate class.  So, just delete the 
"import Excel as xlApp" altogether.

-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list