Fixed font Subject: Bulletproof Exiting Outlook (and not crashing)
Author: Derek Hart Date: 28 Dec 2009
References:
In building an Outlook integration, I try to exit Outlook this way:



Marshal.ReleaseComObject(objOutlook)
objOutlook = Nothing



Before I did this, sometimes this code would not execute, and Outlook would
then get a bit messed up. I would have to load Outlook and wait for it to
repair and/or shut down Outlook in the Task Manager. I don't want this ever
to happen to a user in a commercial application. Is the above code the best
way to handle this scenario. I want to bulletproof an application with
Outlook, and I don't want to corrupt a user's Outlook file.



I am just using standard VB .Net code. Here is a sample. Anything that can
make it safer than my code here?



Try

Dim objOutlook As Outlook.Application

Dim objNameSpace As Outlook.NameSpace

Dim objFolder As Outlook.MAPIFolder

Dim objItem As Outlook.MailItem



objOutlook = New Outlook.Application

objNameSpace = objOutlook.GetNamespace("MAPI")

objNameSpace.Logon()

objFolder =
objNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)



' Do email processing



Catch ex As Exception

MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Information)



Finally

Marshal.ReleaseComObject(objOutlook)

objOutlook = Nothing
28 Dec 2009Bulletproof Exiting Outlook (and not crashing)Derek Hart
28 Dec 2009\ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
28 Dec 2009   \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
29 Dec 2009      \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
29 Dec 2009         \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
29 Dec 2009            \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
29 Dec 2009               \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
29 Dec 2009                  \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
29 Dec 2009                     \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
30 Dec 2009                        \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
30 Dec 2009                           |- Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
30 Dec 2009                           \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
04 Jan 2010                              \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
04 Jan 2010                                 |- Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
04 Jan 2010                                 \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
04 Jan 2010                                    \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
04 Jan 2010                                       \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
05 Jan 2010                                          \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
05 Jan 2010                                             |- Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
05 Jan 2010                                             \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
05 Jan 2010                                                \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
05 Jan 2010                                                   \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
05 Jan 2010                                                      \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
05 Jan 2010                                                         \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
06 Jan 2010                                                            \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...
06 Jan 2010                                                               \ Re: Bulletproof Exiting Outlook (and not crashing)Derek Hart
06 Jan 2010                                                                  \ Re: Bulletproof Exiting Outlook (and not crashing)Ken Slovak - [MVP - O...