outlookAddin.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 demonstrates
  5. # how to plug in to Outlook itself, and hook outlook events.
  6. #
  7. # Additionally, each time a new message arrives in the Inbox, a message
  8. # is printed with the subject of the message.
  9. #
  10. # To register the addin, simply execute:
  11. # outlookAddin.py
  12. # This will install the COM server, and write the necessary
  13. # AddIn key to Outlook
  14. #
  15. # To unregister completely:
  16. # outlookAddin.py --unregister
  17. #
  18. # To debug, execute:
  19. # outlookAddin.py --debug
  20. #
  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-register
  25. # the addin to not automatically load next time Outlook starts. To
  26. # correct this, simply re-register the addin (see above)
  27. from win32com import universal
  28. from win32com.server.exception import COMException
  29. from win32com.client import gencache, DispatchWithEvents
  30. import winerror
  31. import pythoncom
  32. from win32com.client import constants
  33. import sys
  34. # Support for COM objects we use.
  35. gencache.EnsureModule('{00062FFF-0000-0000-C000-000000000046}', 0, 9, 0, bForDemand=True) # Outlook 9
  36. gencache.EnsureModule('{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}', 0, 2, 1, bForDemand=True) # Office 9
  37. # The TLB defining the interfaces we implement
  38. universal.RegisterInterfaces('{AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}', 0, 1, 0, ["_IDTExtensibility2"])
  39. class ButtonEvent:
  40. def OnClick(self, button, cancel):
  41. import win32ui # Possible, but not necessary, to use a Pythonwin GUI
  42. win32ui.MessageBox("Hello from Python")
  43. return cancel
  44. class FolderEvent:
  45. def OnItemAdd(self, item):
  46. try:
  47. print("An item was added to the inbox with subject:", item.Subject)
  48. except AttributeError:
  49. print("An item was added to the inbox, but it has no subject! - ", repr(item))
  50. class OutlookAddin:
  51. _com_interfaces_ = ['_IDTExtensibility2']
  52. _public_methods_ = []
  53. _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER
  54. _reg_clsid_ = "{0F47D9F3-598B-4d24-B7E3-92AC15ED27E2}"
  55. _reg_progid_ = "Python.Test.OutlookAddin"
  56. _reg_policy_spec_ = "win32com.server.policy.EventHandlerPolicy"
  57. def OnConnection(self, application, connectMode, addin, custom):
  58. print("OnConnection", application, connectMode, addin, custom)
  59. # ActiveExplorer may be none when started without a UI (eg, WinCE synchronisation)
  60. activeExplorer = application.ActiveExplorer()
  61. if activeExplorer is not None:
  62. bars = activeExplorer.CommandBars
  63. toolbar = bars.Item("Standard")
  64. item = toolbar.Controls.Add(Type=constants.msoControlButton, Temporary=True)
  65. # Hook events for the item
  66. item = self.toolbarButton = DispatchWithEvents(item, ButtonEvent)
  67. item.Caption="Python"
  68. item.TooltipText = "Click for Python"
  69. item.Enabled = True
  70. # And now, for the sake of demonstration, setup a hook for all new messages
  71. inbox = application.Session.GetDefaultFolder(constants.olFolderInbox)
  72. self.inboxItems = DispatchWithEvents(inbox.Items, FolderEvent)
  73. def OnDisconnection(self, mode, custom):
  74. print("OnDisconnection")
  75. def OnAddInsUpdate(self, custom):
  76. print("OnAddInsUpdate", custom)
  77. def OnStartupComplete(self, custom):
  78. print("OnStartupComplete", custom)
  79. def OnBeginShutdown(self, custom):
  80. print("OnBeginShutdown", custom)
  81. def RegisterAddin(klass):
  82. import winreg
  83. key = winreg.CreateKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\Addins")
  84. subkey = winreg.CreateKey(key, klass._reg_progid_)
  85. winreg.SetValueEx(subkey, "CommandLineSafe", 0, winreg.REG_DWORD, 0)
  86. winreg.SetValueEx(subkey, "LoadBehavior", 0, winreg.REG_DWORD, 3)
  87. winreg.SetValueEx(subkey, "Description", 0, winreg.REG_SZ, klass._reg_progid_)
  88. winreg.SetValueEx(subkey, "FriendlyName", 0, winreg.REG_SZ, klass._reg_progid_)
  89. def UnregisterAddin(klass):
  90. import winreg
  91. try:
  92. winreg.DeleteKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\Addins\\" + klass._reg_progid_)
  93. except WindowsError:
  94. pass
  95. if __name__ == '__main__':
  96. import win32com.server.register
  97. win32com.server.register.UseCommandLine(OutlookAddin)
  98. if "--unregister" in sys.argv:
  99. UnregisterAddin(OutlookAddin)
  100. else:
  101. RegisterAddin(OutlookAddin)