0001 # A demo plugin for Microsoft Excel 0002 # 0003 # This addin simply adds a new button to the main Excel toolbar, 0004 # and displays a message box when clicked. Thus, it demonstrates 0005 # how to plug in to Excel itself, and hook Excel events. 0006 # 0007 # 0008 # To register the addin, simply execute: 0009 # excelAddin.py 0010 # This will install the COM server, and write the necessary 0011 # AddIn key to Excel 0012 # 0013 # To unregister completely: 0014 # excelAddin.py --unregister 0015 # 0016 # To debug, execute: 0017 # excelAddin.py --debug 0018 # 0019 # Then open Pythonwin, and select "Tools->Trace Collector Debugging Tool" 0020 # Restart excel, and you should see some output generated. 0021 # 0022 # NOTE: If the AddIn fails with an error, Excel will re-register 0023 # the addin to not automatically load next time Excel starts. To 0024 # correct this, simply re-register the addin (see above) 0025 # 0026 # Author <ekoome@yahoo.com> Eric Koome 0027 # Copyright (c) 2003 Wavecom Inc. All rights reserved 0028 # 0029 # Redistribution and use in source and binary forms, with or without 0030 #modification, are permitted provided that the following conditions 0031 #are met: 0032 # 0033 #1. Redistributions of source code must retain the above copyright 0034 # notice, this list of conditions and the following disclaimer. 0035 # 0036 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 0037 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0038 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 0039 # DISCLAIMED. IN NO EVENT SHALL ERIC KOOME OR 0040 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0041 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0042 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 0043 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 0044 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 0045 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 0046 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 0047 # SUCH DAMAGE. 0048 0049 from win32com import universal 0050 from win32com.server.exception import COMException 0051 from win32com.client import gencache, DispatchWithEvents 0052 import winerror 0053 import pythoncom 0054 from win32com.client import constants, Dispatch 0055 import sys 0056 0057 # Support for COM objects we use. 0058 gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 3, bForDemand=True) # Excel 9 0059 gencache.EnsureModule('{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}', 0, 2, 1, bForDemand=True) # Office 9 0060 0061 # The TLB defiining the interfaces we implement 0062 universal.RegisterInterfaces('{AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}', 0, 1, 0, ["_IDTExtensibility2"]) 0063 class ButtonEvent: 0064 def OnClick(self, button, cancel): 0065 import win32ui # Possible, but not necessary, to use a Pythonwin GUI 0066 import win32con 0067 win32ui.MessageBox("Hello from Python", "Python Test",win32con.MB_OKCANCEL) 0068 return cancel 0069 0070 class ExcelAddin: 0071 _com_interfaces_ = ['_IDTExtensibility2'] 0072 _public_methods_ = [] 0073 _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER 0074 _reg_clsid_ = "{C5482ECA-F559-45A0-B078-B2036E6F011A}" 0075 _reg_progid_ = "Python.Test.ExcelAddin" 0076 _reg_policy_spec_ = "win32com.server.policy.EventHandlerPolicy" 0077 0078 def __init__(self): 0079 self.appHostApp = None 0080 0081 def OnConnection(self, application, connectMode, addin, custom): 0082 print "OnConnection", application, connectMode, addin, custom 0083 try: 0084 self.appHostApp = application 0085 cbcMyBar = self.appHostApp.CommandBars.Add(Name="PythonBar", Position=constants.msoBarTop, MenuBar=constants.msoBarTypeNormal, Temporary=True) 0086 btnMyButton = cbcMyBar.Controls.Add(Type=constants.msoControlButton, Parameter="Greetings") 0087 btnMyButton=self.toolbarButton = DispatchWithEvents(btnMyButton, ButtonEvent) 0088 btnMyButton.Style = constants.msoButtonCaption 0089 btnMyButton.BeginGroup = True 0090 btnMyButton.Caption = "&Python" 0091 btnMyButton.TooltipText = "Python rules the World" 0092 btnMyButton.Width = "34" 0093 cbcMyBar.Visible = True 0094 except pythoncom.com_error, (hr, msg, exc, arg): 0095 print "The Excel call failed with code %d: %s" % (hr, msg) 0096 if exc is None: 0097 print "There is no extended error information" 0098 else: 0099 wcode, source, text, helpFile, helpId, scode = exc 0100 print "The source of the error is", source 0101 print "The error message is", text 0102 print "More info can be found in %s (id=%d)" % (helpFile, helpId) 0103 0104 def OnDisconnection(self, mode, custom): 0105 print "OnDisconnection" 0106 self.appHostApp.CommandBars("PythonBar").Delete 0107 self.appHostApp=None 0108 0109 def OnAddInsUpdate(self, custom): 0110 print "OnAddInsUpdate", custom 0111 def OnStartupComplete(self, custom): 0112 print "OnStartupComplete", custom 0113 def OnBeginShutdown(self, custom): 0114 print "OnBeginShutdown", custom 0115 0116 def RegisterAddin(klass): 0117 import _winreg 0118 key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Excel\\Addins") 0119 subkey = _winreg.CreateKey(key, klass._reg_progid_) 0120 _winreg.SetValueEx(subkey, "CommandLineSafe", 0, _winreg.REG_DWORD, 0) 0121 _winreg.SetValueEx(subkey, "LoadBehavior", 0, _winreg.REG_DWORD, 3) 0122 _winreg.SetValueEx(subkey, "Description", 0, _winreg.REG_SZ, "Excel Addin") 0123 _winreg.SetValueEx(subkey, "FriendlyName", 0, _winreg.REG_SZ, "A Simple Excel Addin") 0124 0125 def UnregisterAddin(klass): 0126 import _winreg 0127 try: 0128 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Excel\\Addins\\" + klass._reg_progid_) 0129 except WindowsError: 0130 pass 0131 0132 if __name__ == '__main__': 0133 import win32com.server.register 0134 win32com.server.register.UseCommandLine(ExcelAddin) 0135 if "--unregister" in sys.argv: 0136 UnregisterAddin(ExcelAddin) 0137 else: 0138 RegisterAddin(ExcelAddin) 0139
Generated by PyXR 0.9.4