Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

outlookAddin.py 4.6KB

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