Changing Outlook mail message MessageClass from Add-in

M

MoRs

Hi,

I have a Outlook add-in that adds the the menu item in the menu bar. When
the user selects the menu item, my code is executed and changes the
MessageClass for the selected MailItem.
Now the problem - when the code finishes and I try to open the selected
MailItem the standard outlook mail form is shown. Outlook Spy showns that
the MailItem.MessageClass has the new value, while
MailItem.FormDescription.MessageClass still has the value "IPM.Note".
As I understood the MailItem.FormDescription holds the information about the
form used to display the item. So is there any way to refresh/reload the
MailItem.FormDescription?

I can even replicate this behaviour through Outlook Spy by changing
PR_MESSAGE_CLASS property. Maybe I am researching the wrong way?

There is the code that is executed:

Outlook._MailItem item = null;
item = m_outlookApp.ActiveExplorer().Selection[1] as Outlook._MailItem;

if ( !item.MessageClass.Equals("IPM.Note.NewNETTest") )
{
item.MessageClass = "IPM.Note.NewNETTest";
item.Save();
item.Close(Outlook.OlInspectorClose.olSave);

string entryID = item.EntryID;

item = null;
item =
(Outlook._MailItem)m_outlookApp.GetNamespace("MAPI").GetItemFromID(entryID,
System.Type.Missing);
item.Display(false);
}
 
T

Tom Rizzo [MSFT]

I wonder if you're one-offing the item. Do you see the size of the item
change significantly after you save it?

Tom
 
M

MoRs

No, the size stays the same. When I close and restart the Outlook then
MailItem.FormDescription.MessageClass is changed to "IPM.Note.NEWNETTest".
It seems that I need to refresh/reload MailItem.FormDescription but I
haven't found any info how to do this.

Tom Rizzo said:
I wonder if you're one-offing the item. Do you see the size of the item
change significantly after you save it?

Tom

--
Looking for a good book on programming Exchange, Outlook, ADSI and
SharePoint? Check out http://www.microsoft.com/MSPress/books/5517.asp

This posting is provided "AS IS" with no warranties, and confers no rights.



MoRs said:
Hi,

I have a Outlook add-in that adds the the menu item in the menu bar. When
the user selects the menu item, my code is executed and changes the
MessageClass for the selected MailItem.
Now the problem - when the code finishes and I try to open the selected
MailItem the standard outlook mail form is shown. Outlook Spy showns that
the MailItem.MessageClass has the new value, while
MailItem.FormDescription.MessageClass still has the value "IPM.Note".
As I understood the MailItem.FormDescription holds the information about
the
form used to display the item. So is there any way to refresh/reload the
MailItem.FormDescription?

I can even replicate this behaviour through Outlook Spy by changing
PR_MESSAGE_CLASS property. Maybe I am researching the wrong way?

There is the code that is executed:

Outlook._MailItem item = null;
item = m_outlookApp.ActiveExplorer().Selection[1] as Outlook._MailItem;

if ( !item.MessageClass.Equals("IPM.Note.NewNETTest") )
{
item.MessageClass = "IPM.Note.NewNETTest";
item.Save();
item.Close(Outlook.OlInspectorClose.olSave);

string entryID = item.EntryID;

item = null;
item =
(Outlook._MailItem)m_outlookApp.GetNamespace("MAPI").GetItemFromID(entryID,
System.Type.Missing);
item.Display(false);
}
 
K

Ken Slovak - [MVP - Outlook]

Are you sure your item object is really being released and purged? It could
be cached.

If this is .NET code the garbage collector might not have run unless you
explicitly have called it. Does it work any better if you use a different
object variable and/or make a change like item.Subject = item.Subject,
item.Save and then set MessageClass again before releasing the object?
 
M

MoRs

Thanks for your answer, Ken.
The AddIn code is written in C#. I've tried to change the code as you
suggested, but the result is still the same. When the item is opened,
standard mail form is shown. I am including the source code with your
suggestions. Please review it, maybe I missunderstood something. I think the
problem is that Outlook, or the library between .NET and Outlook, fails to
see that the MessageClass property has changed.

Outlook._MailItem item = null;
Outlook._MailItem newItem = null;
item = m_outlookApp.ActiveExplorer().Selection[1] as Outlook._MailItem;

if ( !item.MessageClass.Equals("IPM.Note.NewNETTest") )
{
item.MessageClass = "IPM.Note.NewNETTest";
item.Save();
string entryID = item.EntryID;
item.Close(MSOutlook.OlInspectorClose.olSave);

item.Subject = item.Subject;
item.Save();

item.MessageClass = "IPM.Note.NewNETTest";
item.Save();

int count =
System.Runtime.InteropServices.Marshal.ReleaseComObject(item);
while ( count != 0 )
count =
System.Runtime.InteropServices.Marshal.ReleaseComObject(item);

item = null;
GC.Collect();

newItem =
(MSOutlook._MailItem)m_outlookApp.GetNamespace("MAPI").GetItemFromID(entryID
, System.Type.Missing);
newItem.Display(false);
}
 
K

Ken Slovak - [MVP - Outlook]

I don't see anything wrong. See when you get the item again what the message
class is and if it's not correct then change it and see what happens. You
are sure that the custom form and its message class are published and
allowable for the folder the item is in?
 
M

MoRs

When I get the item again the MessageClass is correct but the
newItem.FormDescription.MessageClass still has the value "IPM.Note". The
custom form is published into "Personal Forms Library". So I guess the form
is available in the Inbox and its subfolders. If I do newItem = item.Copy()
and then newItem.Display(), the custom form is used. So I believe that the
problem is somehow related to the Outlook item caching.
 
K

Ken Slovak - [MVP - Outlook]

The only things I can think of would be to try using a DoEvents statement
after you release the initial item and before you instantiate the second
one, or returning the EntryID of the item as a function return and opening
the item in a different procedure. That might help with any caching
problems.
 
M

MoRs

Thanks for your help, Ken. Your last suggestions didn't changed the result.
I found an interesting thing - when you move the item to the other folder,
outlook refreshes the information stored in the MailItem.FormDescription.
When the moved item is opened my custom form is shown. So basically, I
create the temporary folder, move the item into the temporary folder, move
it back to the original folder, delete the temporary folder and show the
item. So far so good :)
Now I have to solve several other things:
1. The custom form is opened in "Compose" mode not "Read". Outlook shows
that the message is still not sent and the "To", "CC" and "Subject" fields
are editable.
2. The selected item is changed.

Have you got any suggestions how to open the item in "Read mode"? Or how to
changes the selected item in ActiveExlorer? I think eventually I will find
the answers myself but any push in the right direction will be greatly
appreciated ;)

Thanks again for your help
 
K

Ken Slovak - [MVP - Outlook]

Something is very odd here. If the form item already exists then it should
open in read mode. Only new items or items opened for editing should be in
compose mode. I don't use .NET code for Outlook coding so I can't test what
you are seeing but in Outlook VBA code the process works correctly without
any fancy workarounds.

I created a simple custom email form I named "form_tester". So the
MessageClass = "IPM.Note.form_tester". I published it to the Personal Forms
Library. I then wrote and ran this code after selecting an incoming email I
had received. When the item was opened in code the correct custom form was
displayed. The item was opened in read mode and I had no problems with
ActiveExplorer.Selection.

The custom form has 3 fields on P.2: Conversation, Flag Status and Follow Up
Flag .

The code I used was this:

Public Sub ChangeMC()
Dim oMail As Outlook.MailItem

Set oMail = Application.ActiveExplorer.Selection(1)

oMail.MessageClass = "IPM.Note.form_tester"
oMail.Save

Set oMail = Nothing

DoEvents

Set oMail = Application.ActiveExplorer.Selection(1)
oMail.Display

Set oMail = Nothing
End Sub

Are you sure that when you published the custom form you didn't check the
box for including the form description with the item? That would one-off the
item when the message class was changed. It sure sounds like the items are
being one-offed.
 
M

MoRs

yeah something is very very odd :)
I tried your scenario on 4 computers (three with WinXPPro and Outlook2003,
fourth with WinXPPro and Outlook2002). The MessageClass was changed always
(the icon in the message list is changed) but the standard "IPM.Note" form
is displayed. When I deselect the item, some progress dialog is briefly
displayed. Then I select the same item again and now the custom form is
shown. Something strange is happening. One of my colleagues suggests that
the progress bar is displayed when the custom form is beeing cached, and
only then the item is shown with the custom form.
Btw I am not really experienced with VBA coding. Where this code needs to be
put exactly? I created a new macro, named it "ChangeMC" and then used
"Macro/Macros/Run" to run the code. Is this the right way?
 
K

Ken Slovak - [MVP - Outlook]

Yes, if you briefly see a progress bar it most likely means that the form
wasn't cached and is now being loaded into the cache.

I put the code in an Outlook VBA code module and put the cursor in the code
and pressed F5 to run it but your method runs the macro too. It runs here so
any problems you're having with it definitely indicates a problem in your
system.

First make sure the send form design with item checkbox is not checked in
the Properties tab of the form when it's in design mode. And say no when
publishing the form to the same question. Then make sure that you are using
the version property and incrementing it or changing it every time you
publish your form. I use a string starting at 1.00 and treat it as a number
and increment it every time I publish, that prevents forms cache corruption.
That said I'd also clear the forms cache to start with - Tools, Options,
Other tab, Advanced Options button, Custom Forms button, Manage Forms
button, then click the Clear Cache button.
 
M

MoRs

Thanks for your help, Ken. I really appreciate it :)
The form versioning was a new information to me. Still I am getting the same
behavior. The custom form is saved without "Send form design with item".
When the macro finishes the MessageClass is changed, but the item is opened
using "IPM.Note" form. When I change the selection by clicking on the other
item, then select the changed item again the custom form is displayed.
Could you tell me the versions of the Windows and Office you are using? I
want to try to setup the system that resembles yours as much as possible and
see if it works :) I installed Office 2003 SP1 but it didn't changed the
situation.
 
K

Ken Slovak - [MVP - Outlook]

At this point I have no idea why you're having the problems you are having.
It should be straightforward, but it's obviously some sort of caching
problem.

I tested that macro on Outlook 2000 SP3 on Windows 2000 SP4 and also on
Outlook 2003 SP1 on Windows 2000 SP4. I didn't test on Outlook 2002 or on
Windows XP.
 
M

MoRs

Thanks for your help, Ken. You helped me a lot to understand the whole
situation. I will try to test on as many different setups as possible. Maybe
I will find the one that works :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top