D
Derek Hart
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
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