.msg file cannot be opened more than once

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

.msg file cannot be opened more than once
Opening an .msg file from outside Outlook 
Benn Kjær




Posts: 28
Joined: 2014-07-24
Hi.

I have created an outlook add-in for our customers to use with Outlook 2010. But a strange issue has turned up.

I you open an .msg file from outside Outlook it opens/closes fine the first time. But if you try to open it again you get the message "Unable to open file. Already in use".

The message is generated by Outlook.

When a mail is opened inside Outlook there is no issue, no matter how many times it get opened.

If I then uninstall my add-in, the .msg file can be accessed as may times as wanted.

Do any of you have an idea of what may course this problem?
Posted 22 Jun, 2016 04:32:44 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Benn,

Does opening the .MSG file (Outlook isn't running) starts an OUTLOOK.EXE (see Task Manager)? Does closing the file ends the process? If not, comment our your code to check if the issue is produced by it. If it is the case, make sure you release every COM object in your code; see https://www.add-in-express.com/docs/net-office-tips.php#releasing.


Andrei Smolin
Add-in Express Team Leader
Posted 22 Jun, 2016 05:28:45 Top
Benn Kjær




Posts: 28
Joined: 2014-07-24
Hi Andrei

The error only occurs when Outlook is running.

When Outlook is not running, OUTLOOK.EXE is opened and closed ( In the Taskmanager ) together with the .msg file. No issue here.
Posted 22 Jun, 2016 06:19:46 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Benn,

Does the issue occur if you disable *all* other add-ins?

Does it occur if you create a new empty add-in project?


Andrei Smolin
Add-in Express Team Leader
Posted 22 Jun, 2016 10:05:09 Top
Benn Kjær




Posts: 28
Joined: 2014-07-24
Andrei,

If I disable all non standard Microsift add-in's I get no error.

Let me try to create an Empty add-in.

Regards
Benn
Posted 23 Jun, 2016 01:08:45 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Benn,

Please keep us informed.
Posted 23 Jun, 2016 03:09:39 Top
Benn Kjær




Posts: 28
Joined: 2014-07-24
Hi Add-in Express team

I have been investigating in the described error, and the error is caused by my use of UserProperties in the Email item, even if I try to delete the Userproperty.

With this code it is not possible to reopen a .msg file:


public Outlook.MailItem email = null;

private void adxOutlookAppEvents1_NewInspector( object sender, object inspector, string folderName )
{
   Outlook._Inspector olInsp = ( Outlook._Inspector ) inspector;
   object item = olInsp.CurrentItem;

   if( item != null && item is Outlook.MailItem )
   {
      email = ( Outlook.MailItem ) item;
      email.UserProperties.Add( "MyProperty", Outlook.OlUserPropertyType.olText, null, false );
   }
}

private void adxOutlookAppEvents1_InspectorClose( object sender, object inspector, string folderName )
{
   if( email != null )
   {
      email.UserProperties[ "MyProperty" ].Delete();
      Marshal.ReleaseComObject( email );
   }
}


If the two "email.UserProperties" lines is commented out, is works.

So I will say that this is not a Add-in Express issue, but something in Outlook.
Posted 24 Jun, 2016 07:05:33 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Benn,

You need to release the following COM object created in your code:
- the UserProperties collection created by email.UserProperties (twice!)
- the UserProperty created by UserProperties.Add
- the UserProperty created by UserProperties[ "MyProperty" ]

Also, you need to release object item in case it is an item type other than Outlook.MailItem.


Andrei Smolin
Add-in Express Team Leader
Posted 24 Jun, 2016 09:17:02 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Benn,

Please check https://www.add-in-express.com/forum/read.php?FID=5&TID=13956.


Andrei Smolin
Add-in Express Team Leader
Posted 28 Jun, 2016 06:10:26 Top
Benn Kjær




Posts: 28
Joined: 2014-07-24
Hi Andrei

Interesting. ;-)
Posted 28 Jun, 2016 06:24:28 Top