Can?t open .msg file twice

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

Can?t open .msg file twice
 
dik




Posts: 11
Joined: 2012-08-29
Dear Add-In Express Team,

I hope you are all healthy...

In my Addin, when i open a .msg file twice, i get following error:

We can't open 'c:\...\test.msg'. It's possible the file is already open, or you don't have permission to open it. To check your permissions, right-click the file folder, then click Properties.

I have located the error in the OutlookExplorerSelectItem_InspectorClose event.
Therefore i broke down my code to this:


            Outlook.Inspector currentInspector = (Outlook.Inspector)inspector;

            if (OutlookApp.ActiveInspector() != null)
            {
                Marshal.ReleaseComObject(OutlookApp.ActiveInspector());
            }
            if (currentInspector.CurrentItem != null)
            {
                Marshal.ReleaseComObject(currentInspector.CurrentItem);
            }
            if (currentInspector != null)
            {
                Marshal.ReleaseComObject(currentInspector);
            }
            if (inspector != null)
            {
                Marshal.ReleaseComObject(inspector);
            }
        }


If i delete following line, everything is fine:

Outlook.Inspector currentInspector = (Outlook.Inspector)inspector;

I know, i am doing something wrong with releasing the COM-objects, but i don?t get it...

Can you support me on this?
Posted 08 Apr, 2020 04:29:49 Top
Andrei Smolin


Add-in Express team


Posts: 18819
Joined: 2006-05-11
Hello dik,

dik writes:
if (OutlookApp.ActiveInspector() != null)
{
Marshal.ReleaseComObject(OutlookApp.ActiveInspector());
}
if (currentInspector.CurrentItem != null)
{
Marshal.ReleaseComObject(currentInspector.CurrentItem);
}


These lines cause the issue because *every* call to a property/method returning a COM object creates a new COM object. There are four such calls above while there are only two Marshal.ReleaseComObject calls.


Andrei Smolin
Add-in Express Team Leader
Posted 08 Apr, 2020 05:34:56 Top
dik




Posts: 11
Joined: 2012-08-29
Hi Andrei,

thank you, now it works.
Posted 08 Apr, 2020 06:06:15 Top