PyXR

c:\python24\lib\site-packages\win32 \ com \ demos \ outlookAddin.py



0001 # A demo plugin for Microsoft Outlook (NOT Outlook Express)
0002 #
0003 # This addin simply adds a new button to the main Outlook toolbar,
0004 # and displays a message box when clicked.  Thus, it demonstrates
0005 # how to plug in to Outlook itself, and hook outlook events.
0006 #
0007 # Additionally, each time a new message arrives in the Inbox, a message
0008 # is printed with the subject of the message.
0009 #
0010 # To register the addin, simply execute:
0011 #   outlookAddin.py
0012 # This will install the COM server, and write the necessary
0013 # AddIn key to Outlook
0014 #
0015 # To unregister completely:
0016 #   outlookAddin.py --unregister
0017 #
0018 # To debug, execute:
0019 #   outlookAddin.py --debug
0020 #
0021 # Then open Pythonwin, and select "Tools->Trace Collector Debugging Tool"
0022 # Restart Outlook, and you should see some output generated.
0023 #
0024 # NOTE: If the AddIn fails with an error, Outlook will re-register
0025 # the addin to not automatically load next time Outlook starts.  To
0026 # correct this, simply re-register the addin (see above)
0027 
0028 from win32com import universal
0029 from win32com.server.exception import COMException
0030 from win32com.client import gencache, DispatchWithEvents
0031 import winerror
0032 import pythoncom
0033 from win32com.client import constants
0034 import sys
0035 
0036 # Support for COM objects we use.
0037 gencache.EnsureModule('{00062FFF-0000-0000-C000-000000000046}', 0, 9, 0, bForDemand=True) # Outlook 9
0038 gencache.EnsureModule('{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}', 0, 2, 1, bForDemand=True) # Office 9
0039 
0040 # The TLB defiining the interfaces we implement
0041 universal.RegisterInterfaces('{AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}', 0, 1, 0, ["_IDTExtensibility2"])
0042 
0043 class ButtonEvent:
0044     def OnClick(self, button, cancel):
0045         import win32ui # Possible, but not necessary, to use a Pythonwin GUI
0046         win32ui.MessageBox("Hello from Python")
0047         return cancel
0048 
0049 class FolderEvent:
0050     def OnItemAdd(self, item):
0051         try:
0052             print "An item was added to the inbox with subject:", item.Subject
0053         except AttributeError:
0054             print "An item was added to the inbox, but it has no subject! - ", repr(item)
0055 
0056 
0057 
0058 class OutlookAddin:
0059     _com_interfaces_ = ['_IDTExtensibility2']
0060     _public_methods_ = []
0061     _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER
0062     _reg_clsid_ = "{0F47D9F3-598B-4d24-B7E3-92AC15ED27E2}"
0063     _reg_progid_ = "Python.Test.OutlookAddin"
0064     _reg_policy_spec_ = "win32com.server.policy.EventHandlerPolicy"
0065     def OnConnection(self, application, connectMode, addin, custom):
0066         print "OnConnection", application, connectMode, addin, custom
0067         # ActiveExplorer may be none when started without a UI (eg, WinCE synchronisation)
0068         activeExplorer = application.ActiveExplorer()
0069         if activeExplorer is not None:
0070             bars = activeExplorer.CommandBars
0071             toolbar = bars.Item("Standard")
0072             item = toolbar.Controls.Add(Type=constants.msoControlButton, Temporary=True)
0073             # Hook events for the item
0074             item = self.toolbarButton = DispatchWithEvents(item, ButtonEvent)
0075             item.Caption="Python"
0076             item.TooltipText = "Click for Python"
0077             item.Enabled = True
0078 
0079         # And now, for the sake of demonstration, setup a hook for all new messages
0080         inbox = application.Session.GetDefaultFolder(constants.olFolderInbox)
0081         self.inboxItems = DispatchWithEvents(inbox.Items, FolderEvent)
0082 
0083     def OnDisconnection(self, mode, custom):
0084         print "OnDisconnection"
0085     def OnAddInsUpdate(self, custom):
0086         print "OnAddInsUpdate", custom
0087     def OnStartupComplete(self, custom):
0088         print "OnStartupComplete", custom
0089     def OnBeginShutdown(self, custom):
0090         print "OnBeginShutdown", custom
0091 
0092 def RegisterAddin(klass):
0093     import _winreg
0094     key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\Addins")
0095     subkey = _winreg.CreateKey(key, klass._reg_progid_)
0096     _winreg.SetValueEx(subkey, "CommandLineSafe", 0, _winreg.REG_DWORD, 0)
0097     _winreg.SetValueEx(subkey, "LoadBehavior", 0, _winreg.REG_DWORD, 3)
0098     _winreg.SetValueEx(subkey, "Description", 0, _winreg.REG_SZ, klass._reg_progid_)
0099     _winreg.SetValueEx(subkey, "FriendlyName", 0, _winreg.REG_SZ, klass._reg_progid_)
0100 
0101 def UnregisterAddin(klass):
0102     import _winreg
0103     try:
0104         _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\Addins\\" + klass._reg_progid_)
0105     except WindowsError:
0106         pass
0107 
0108 if __name__ == '__main__':
0109     import win32com.server.register
0110     win32com.server.register.UseCommandLine(OutlookAddin)
0111     if "--unregister" in sys.argv:
0112         UnregisterAddin(OutlookAddin)
0113     else:
0114         RegisterAddin(OutlookAddin)
0115 

Generated by PyXR 0.9.4
SourceForge.net Logo