K
kostas
Hello, i am developing an exe application and i use global object to declare
outlook application withevents. I use outlook 2007 sp2 and i am having
trouble releasing my variable references or reusing then. I use this code :
Public Class StartOE
Private WithEvents o As Outlook.Application = Nothing
Private ns As Outlook.NameSpace = Nothing
Private omFolder As Outlook.MAPIFolder = Nothing
Public Sub New()
If detectOutlook() Then
o = GetObject(, "Outlook.Application")
Else
o = New Outlook.Application
End If
Try
ns = o.GetNamespace("MAPI") ' get namespace
ns.Logon("", "", True, True) 'log into new Outlook session
omFolder =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) 'get folder
If o.Explorers.Count = 0 Then o.Explorers.Add(omFolder) 'show
explorer
Catch ex As Exception
Err.Clear()
End Try
End Sub
Private Sub o_Quit() Handles o.Quit
' MsgBox("quit")
If Not omFolder Is Nothing Then Marshal.ReleaseComObject(omFolder)
If Not ns Is Nothing Then Marshal.ReleaseComObject(ns)
If Not o Is Nothing Then Marshal.ReleaseComObject(o)
omFolder = Nothing
ns = Nothing
o = Nothing 'Causes RPC exception
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Class
The problem starts when the user closes outlook and the event triggers .
I know that when quit event is executed, outlook releases all its references
to external objects, so i am getting RPC exception setting object o to
nothing (see my code). I do not want to prevent users from closing outlook, i
just want to be able to launch outlook again using the same declared objects.
I get this error even if, after the execution of quit event, i try to set "
o = New Outlook.Application" . Can anyone point me to a workaround? I want to
be able to reuse 'o' object using 'new' or getobject.
Thank you in advance
outlook application withevents. I use outlook 2007 sp2 and i am having
trouble releasing my variable references or reusing then. I use this code :
Public Class StartOE
Private WithEvents o As Outlook.Application = Nothing
Private ns As Outlook.NameSpace = Nothing
Private omFolder As Outlook.MAPIFolder = Nothing
Public Sub New()
If detectOutlook() Then
o = GetObject(, "Outlook.Application")
Else
o = New Outlook.Application
End If
Try
ns = o.GetNamespace("MAPI") ' get namespace
ns.Logon("", "", True, True) 'log into new Outlook session
omFolder =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) 'get folder
If o.Explorers.Count = 0 Then o.Explorers.Add(omFolder) 'show
explorer
Catch ex As Exception
Err.Clear()
End Try
End Sub
Private Sub o_Quit() Handles o.Quit
' MsgBox("quit")
If Not omFolder Is Nothing Then Marshal.ReleaseComObject(omFolder)
If Not ns Is Nothing Then Marshal.ReleaseComObject(ns)
If Not o Is Nothing Then Marshal.ReleaseComObject(o)
omFolder = Nothing
ns = Nothing
o = Nothing 'Causes RPC exception
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Class
The problem starts when the user closes outlook and the event triggers .
I know that when quit event is executed, outlook releases all its references
to external objects, so i am getting RPC exception setting object o to
nothing (see my code). I do not want to prevent users from closing outlook, i
just want to be able to launch outlook again using the same declared objects.
I get this error even if, after the execution of quit event, i try to set "
o = New Outlook.Application" . Can anyone point me to a workaround? I want to
be able to reuse 'o' object using 'new' or getobject.
Thank you in advance