codekingpro/portable-devtools
114k
1# A demo plugin for Microsoft Outlook (NOT Outlook Express)2#3# This addin simply adds a new button to the main Outlook toolbar,4# and displays a message box when clicked. Thus, it demonstrates5# how to plug in to Outlook itself, and hook outlook events.6#7# Additionally, each time a new message arrives in the Inbox, a message8# is printed with the subject of the message.9#10# To register the addin, simply execute:11# outlookAddin.py12# This will install the COM server, and write the necessary13# AddIn key to Outlook14#15# To unregister completely:16# outlookAddin.py --unregister17#18# To debug, execute:19# outlookAddin.py --debug20#21# Then open Pythonwin, and select "Tools->Trace Collector Debugging Tool"22# Restart Outlook, and you should see some output generated.23#24# NOTE: If the AddIn fails with an error, Outlook will re-register25# the addin to not automatically load next time Outlook starts. To26# correct this, simply re-register the addin (see above)27 28import sys29 30import pythoncom31from win32com import universal32from win32com.client import DispatchWithEvents, constants, gencache33from win32com.server.exception import COMException34 35# Support for COM objects we use.36gencache.EnsureModule(37 "{00062FFF-0000-0000-C000-000000000046}", 0, 9, 0, bForDemand=True38) # Outlook 939gencache.EnsureModule(40 "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}", 0, 2, 1, bForDemand=True41) # Office 942 43# The TLB defining the interfaces we implement44universal.RegisterInterfaces(45 "{AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}", 0, 1, 0, ["_IDTExtensibility2"]46)47 48 49class ButtonEvent:50 def OnClick(self, button, cancel):51 import win32ui # Possible, but not necessary, to use a Pythonwin GUI52 53 win32ui.MessageBox("Hello from Python")54 return cancel55 56 57class FolderEvent:58 def OnItemAdd(self, item):59 try:60 print("An item was added to the inbox with subject:", item.Subject)61 except AttributeError:62 print(63 "An item was added to the inbox, but it has no subject! - ", repr(item)64 )65 66 67class OutlookAddin:68 _com_interfaces_ = ["_IDTExtensibility2"]69 _public_methods_ = []70 _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER71 _reg_clsid_ = "{0F47D9F3-598B-4d24-B7E3-92AC15ED27E2}"72 _reg_progid_ = "Python.Test.OutlookAddin"73 _reg_policy_spec_ = "win32com.server.policy.EventHandlerPolicy"74 75 def OnConnection(self, application, connectMode, addin, custom):76 print("OnConnection", application, connectMode, addin, custom)77 # ActiveExplorer may be none when started without a UI (eg, WinCE synchronisation)78 activeExplorer = application.ActiveExplorer()79 if activeExplorer is not None:80 bars = activeExplorer.CommandBars81 toolbar = bars.Item("Standard")82 item = toolbar.Controls.Add(Type=constants.msoControlButton, Temporary=True)83 # Hook events for the item84 item = self.toolbarButton = DispatchWithEvents(item, ButtonEvent)85 item.Caption = "Python"86 item.TooltipText = "Click for Python"87 item.Enabled = True88 89 # And now, for the sake of demonstration, setup a hook for all new messages90 inbox = application.Session.GetDefaultFolder(constants.olFolderInbox)91 self.inboxItems = DispatchWithEvents(inbox.Items, FolderEvent)92 93 def OnDisconnection(self, mode, custom):94 print("OnDisconnection")95 96 def OnAddInsUpdate(self, custom):97 print("OnAddInsUpdate", custom)98 99 def OnStartupComplete(self, custom):100 print("OnStartupComplete", custom)101 102 def OnBeginShutdown(self, custom):103 print("OnBeginShutdown", custom)104 105 106def RegisterAddin(klass):107 import winreg108 109 key = winreg.CreateKey(110 winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\Addins"111 )112 subkey = winreg.CreateKey(key, klass._reg_progid_)113 winreg.SetValueEx(subkey, "CommandLineSafe", 0, winreg.REG_DWORD, 0)114 winreg.SetValueEx(subkey, "LoadBehavior", 0, winreg.REG_DWORD, 3)115 winreg.SetValueEx(subkey, "Description", 0, winreg.REG_SZ, klass._reg_progid_)116 winreg.SetValueEx(subkey, "FriendlyName", 0, winreg.REG_SZ, klass._reg_progid_)117 118 119def UnregisterAddin(klass):120 import winreg121 122 try:123 winreg.DeleteKey(124 winreg.HKEY_CURRENT_USER,125 "Software\\Microsoft\\Office\\Outlook\\Addins\\" + klass._reg_progid_,126 )127 except WindowsError:128 pass129 130 131if __name__ == "__main__":132 import win32com.server.register133 134 win32com.server.register.UseCommandLine(OutlookAddin)135 if "--unregister" in sys.argv:136 UnregisterAddin(OutlookAddin)137 else:138 RegisterAddin(OutlookAddin)139 