Shouldn't this be doing it?
Class Level
Dim objOutlook As Outlook.Application
Subroutine
Dim objNameSpace As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem
InstantiateOutlook()
objNameSpace = objOutlook.GetNamespace("MAPI")
objFolder =
objNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
objNameSpace.Logon()
' Do processing
' Release all objects
Marshal.ReleaseComObject(objOutlook)
Marshal.ReleaseComObject(objNameSpace)
Marshal.ReleaseComObject(objFolder)
Marshal.ReleaseComObject(objItem)
objOutlook = Nothing
objNameSpace = Nothing
objFolder = Nothing
objItem = Nothing
' This is the code for InstantiateOutlook
Private Sub InstantiateOutlook()
Try
Dim canQuit As Boolean
Dim olProcess As String = "Outlook.exe"
Dim query As New SelectQuery("SELECT Name FROM Win32_Process
WHERE name='" & olProcess & "'")
Dim searcher As New ManagementObjectSearcher(query)
Dim objectCollection As ManagementObjectCollection =
searcher.Get()
Dim collCount As Integer = objectCollection.Count
searcher.Dispose()
objectCollection.Dispose()
searcher = Nothing
objectCollection = Nothing
If collCount <> 0 Then
' Outlook already running, hook into the Outlook instance
objOutlook =
TryCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)
If objOutlook IsNot Nothing Then
canQuit = False
End If
Else
' Outlook not already running, start it
Dim _app As New Outlook.ApplicationClass()
objOutlook = DirectCast(_app, Outlook.Application)
End If
SecurityManager1.ConnectTo(objOutlook)
SecurityManager1.DisableOOMWarnings = True
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Sub
"Ken Slovak - [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:OZKL%(E-Mail Removed)...
> If Outlook is not completely exiting as a result of running your code it
> means you aren't releasing all your Outlook objects. You need to release
> every one of them. It doesn't matter from where your shutdown code is
> called, from an Explorer or Inspector Close() event or Application.Quit(),
> or anywhere else. You just release everything.
>
> --
> Ken Slovak
> [MVP - Outlook]
> http://www.slovaktech.com
> Author: Professional Programming Outlook 2007.
> Reminder Manager, Extended Reminders, Attachment Options.
> http://www.slovaktech.com/products.htm
>
>
> "Derek Hart" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> When I run this process, it works fine the first time. After the first
>> run, Outlook is running. Then let's say the user exits Outlook. It is
>> still in the task manager. Then when your code looks to see if it is
>> there, it is. But then the code tries to get the Outlook with this line:
>>
>> objOutlook = TryCast(Marshal.GetActiveObject("Outlook.Application"),
>> Outlook.Application)
>>
>> But it is not really there. So the code errors. Should I trap for that
>> and then get Outlook the other way:
>> Dim _app As New Outlook.ApplicationClass()
>> objOutlook = DirectCast(_app, Outlook.Application)
>>
>> I guess the global variable keeps Outlook in the task manager, but even
>> when I was not doing this process Outlook still stayed in the task
>> manager. It never seems to exit, even with this:
>> Marshal.ReleaseComObject(objOutlook)
>> objOutlook = Nothing
>>
>> And is there code to exit it properly in the Explorer and Inspector
>> Close() events? I was unclear on this... I am not displaying Outlook UI,
>> but just WindowsForms windows that read and write data to/from the pst
>> file. Do I have to use these events to properly close and get rid of
>> Outlook in the task manager?
>>
>>
>