{"id":28,"date":"2006-10-15T22:16:43","date_gmt":"2006-10-15T21:16:43","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=28"},"modified":"2006-10-16T01:41:53","modified_gmt":"2006-10-16T00:41:53","slug":"what-happens-to-outlook-add-ins-when-outlook-goes-away","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=28","title":{"rendered":"What happens to Outlook add-ins when Outlook goes away?"},"content":{"rendered":"<p>I tried writing <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=23\" title=\"earlier blog post: an app a day...\">another Outlook extension<\/a> tonight. This time, a <a href=\"http:\/\/dalelane.co.uk\/tasktoday\" title=\"download the app\" target=\"_blank\">desktop widget<\/a> to display a dashboard-style at-a-glance view of how busy I am (based on my Microsoft Outlook task list).<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/dalelane.co.uk\/database\/imagedata\/tasktoday.gif\" alt=\"screenshot of the app\" align=\"left\" hspace=\"10\" vspace=\"5\"\/> I wrote it to run in the background, and update itself once a minute. This raised an interesting question &#8211; what to do with the handle to Outlook. <\/p>\n<p>Getting the handle to Outlook is probably the slowest bit of the app, so I don&#8217;t want to do this every minute. But caching a handle and reusing it ad-infinitum probably isn&#8217;t safe &#8211; what if Outlook is closed or restarted, (or&#8230; whatever happens when Windows hibernates?) while the widget app is running? Would the widget hold locks on the Outlook data file and cause a closing Outlook to hang? Or would the widget just fall over the next time it tried to use an invalid handle to the Outlook object model? <\/p>\n<p><!--more--><\/p>\n<p>A quick bit of testing seemed to suggest that the latter is more often the case. Restarting Outlook while my widget was running caused it to throw a bunch of exceptions. <\/p>\n<p>An <a href=\"http:\/\/blogs.msdn.com\/eric_carter\/archive\/2004\/10\/01\/236752.aspx\" title=\"MSDN blog: .NET4Office\" target=\"_blank\">MSDN blog by Eric Carter<\/a> comes at the problem from the other side &#8211; Outlook add-ins which hang around after you&#8217;ve closed Outlook, when you don&#8217;t want them to:<\/p>\n<blockquote><p>&#8230;getting Outlook to shutdown when you write an add-in is pretty hard&#8230; If you have outstanding references to Outlook OM objects (which you almost always will) the Outlook add-in doesn&#8217;t get shutdown because it is waiting for you to release your Outlook OM objects before it shuts you down&#8230;<\/p><\/blockquote>\n<p>The solution, though, is more or less the same &#8211; register listeners for these Outlook shutdown and startup events. When they happen, handle them by making sure that your app trashes it&#8217;s own handles to Outlook. Eric even gives some <a href=\"http:\/\/blogs.msdn.com\/eric_carter\/archive\/2004\/10\/10\/240568.aspx\" title=\"MSDN blog: .NET4Office\" target=\"_blank\">sample code<\/a> which shows how this can be done. <\/p>\n<p>In my case, if Outlook is then not running, I can then create a new instance of an Outlook app for use by my widget. By trashing my handle to the Outlook that the user has closed, I make sure that I don&#8217;t stop Outlook from completing it&#8217;s close operation if it needs to, and that my widget wont start using handles to Outlook which are no longer valid. <\/p>\n<p>If Outlook is started while the widget is running, it captures the startup event. If it&#8217;s handle to Outlook is different, it can then trash it&#8217;s own handle in favour of the currently running instance. <\/p>\n<p>Looks like a bit of a pain for a developer to have to bother with this, but after a quick bit of sanity testing, it seems to do the trick. To be honest, it looks as if this is a problem which might have been solved with later versions of Visual Studio (I&#8217;m still on Visual Studio .NET 2003) &#8211; as I&#8217;ve seen references to Outlook Support in <a href=\"http:\/\/go.microsoft.com\/?linkid=3064044\">Visual Studio 2005 Tools for Office<\/a> which look like this sort of thing has been addressed. <\/p>\n<p>PS &#8211; Okay&#8230; so I haven&#8217;t quite done an <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=23\" title=\"earlier blog post: an app a day...\">app a day<\/a> as I talked about before. I haven&#8217;t really set aside the time to do it. An app a week isn&#8217;t too bad though! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I tried writing another Outlook extension tonight. This time, a desktop widget to display a dashboard-style at-a-glance view of how busy I am (based on my Microsoft Outlook task list). I wrote it to run in the background, and update itself once a minute. This raised an interesting question &#8211; what to do with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=28"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}