itemEvents.RemoveConnection()

Add-in Express™ Support Service
That's what is more important than anything else

itemEvents.RemoveConnection()
which is the better approach? 
Gordon Prince


Guest


I'm getting occasional errors thrown pointing to this procedure. It doesn't happen very often, and I can't reproduce it in my development environment. But looking at the code
    Private Sub AdxOutlookAppEvents1_ExplorerActivate(sender As Object, explorer As Object) Handles _
                AdxOutlookAppEvents1.ExplorerActivate, _
                AdxOutlookAppEvents1.ExplorerSelectionChange
        Dim myExplorer As Outlook.Explorer = TryCast(explorer, Outlook.Explorer)
        If myExplorer Is Nothing Then Return
        Dim sel As Outlook.Selection = Nothing
        Dim item As Object = Nothing
        Try
            sel = myExplorer.Selection
        Catch
        End Try
        If sel Is Nothing Then Return
        Try
            If sel.Count = 1 Then
                item = sel.Item(1)
                If itemEvents.IsConnected Then itemEvents.RemoveConnection()
                itemEvents.ConnectTo(item, True)
            End If
        Catch ex As Exception
        Finally
            If sel IsNot Nothing Then Marshal.ReleaseComObject(sel) : sel = Nothing
        End Try
    End Sub

I'm wondering if this isn't better practice (to RemoveConnection regardless of whether ConnectTo is called):
        Try
            If itemEvents.IsConnected Then itemEvents.RemoveConnection()
            If sel.Count = 1 Then
                item = sel.Item(1)
                itemEvents.ConnectTo(item, True)
            End If
        Catch ex As Exception

Any thoughts?
Posted 24 Nov, 2015 17:09:27 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Gordon,

Gordon Prince writes:
I'm wondering if this isn't better practice (to RemoveConnection regardless of whether ConnectTo is called):


This variant looks more correct to me.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Nov, 2015 02:57:51 Top
Gordon Prince


Guest


Here's the error the user is getting in production. It's quite intermittent, and I can't reproduce it either on the production computer or my development computer. But at least two of my users get it sometimes, so I'd like to fix it.
Exception Source:      GKBMOutlook
Exception Type:        System.Runtime.InteropServices.InvalidComObjectException
Exception Message:     COM object that has been separated from its underlying RCW cannot be used.
Exception Target Site: AdxOutlookAppEvents1_ExplorerActivate

---- Stack Trace ----
   GKBMOutlook.AddinModule.AdxOutlookAppEvents1_ExplorerActivate(sender As Object, explorer As Object)
       GKBMOutlook.DLL: N 0001 (0x1) IL 
   AddinExpress.MSO.ADXOutlookAppEvents.DoExplorerSelectionChange(explorer As Object)
       GKBMOutlook.DLL: N 0017 (0x11) IL 

...
Exception Source:      
Exception Type:        AddinExpress.MSO.ADXExternalException
Exception Message:     An error has occurred in the code of the add-in.
Exception Target Site: Object reference not set to an instance of an object.

I'm wondering about this code for the procedure:

    Private Sub AdxOutlookAppEvents1_ExplorerActivate(sender As Object, explorer As Object) Handles _
                AdxOutlookAppEvents1.ExplorerActivate, _
                AdxOutlookAppEvents1.ExplorerSelectionChange
        Dim myExplorer As Outlook.Explorer = Nothing
        Try
            myExplorer = explorer
        Catch ex As Exception
        End Try
        If myExplorer Is Nothing Then Return

        Dim sel As Outlook.Selection = Nothing
        Try
            sel = myExplorer.Selection
        Catch ex As Exception
        End Try
        If sel Is Nothing Then Return

        Dim item As Object = Nothing
        Try
            If itemEvents.IsConnected Then itemEvents.RemoveConnection()
            If sel.Count = 1 Then
                item = sel.Item(1)
                itemEvents.ConnectTo(item, True)
            End If
        Catch ex As Exception
        Finally
            If sel IsNot Nothing Then Marshal.ReleaseComObject(sel) : sel = Nothing
        End Try
    End Sub

I can't figure out what could be throwing the error. Any ideas?

Also, after contemplating this closely, I wonder if the
If itemEvents.IsConnected Then itemEvents.RemoveConnection()
statement wouldn't be better at the top of the procedure. Any thoughts on that modification?

Thanks.
Posted 25 Nov, 2015 05:59:44 Top
Gordon Prince


Guest


My original code was:
Dim myExplorer As Outlook.Explorer = TryCast(explorer, Outlook.Explorer)

Is that more likely to throw the error than the code I've posted with the separate
Dim var = nothing
var = something
if var is nothing

Thanks.
Posted 25 Nov, 2015 06:05:42 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Gordon Prince writes:
COM object that has been separated from its underlying RCW cannot be used.


This occurs when you use a variable that you previously released using Marshal.ReleaseComObject(). I strongly recommend that you pay attention to all arguments of Add-in Express events: you must not release them. Note that assigning such an argument to a variable (myExplorer = explorer in your code above) does NOT create another COM object; instead both variable now refer to the same COM object and releasing one of them will release the other one.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Nov, 2015 07:03:29 Top
Gordon Prince


Guest


I can't believe it, but I went back through my code and found two objects that Add-in Express passed in to procedures that I released. I've removed those two release statements per your suggestion.

Often it's the basics, eh?
Posted 25 Nov, 2015 07:29:41 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Such thing happen.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Nov, 2015 07:39:04 Top