Addin loading custom outlook forms by default

  • Thread starter Thread starter sublimese
  • Start date Start date
S

sublimese

I am writing an Addin for Outlook 2003 using C#. I am wondering if
anyone can answer a question or point me in the proper direction.

When my addin loads in Outlook, I want custom Outlook forms to be used
instead of the default Outlook forms. For example, when someone goes
to their inbox and opens an email (or presses the "New" button to make
an new email), when my addin is loaded, I want Outlook to load
"IPM.Email.MyCustomEmail", when/if my addin is not loaded, I would want
Outlook to load its default form. Is there a way to do this? I have
looked into trying to iterate the folders collection and setting the
default message type (which does not work because that property is read
only). The ideal solution would be to hook the form opening event in
my addin and have it open my custom form and cancel the opening of the
default form. If the ideal solution is not possible, dynamically
setting the MessageType on each folder would work as well.
Thank you very much,
Kelly Johnson
 
Some basic Outlook principles and thoughts on how they might affect your
project:

1) You cannot set a message form as the default form for a folder. It would
be meaningless, since messages are designed to be sent, not saved.

2) Changing the default form for a folder has no effect on existing items.
(Remember that Outlook is a semi-structured database and allows
heterogeneous items in a single folder.)

3) You can make a registry change to substitute your custom form for the
default form. See
http://www.outlookcode.com/d/newdefaultform.htm#changedefault . However, it
doesn't work completely for message forms in Outlook 2003 and so this method
would probably not be suitable.

4) To change the form used for an individual item, you need to change the
MessageClass of that item. This can be tricky when you're doing it on the
fly. In your case, you could use the NewInspector event to detect the item
opening, change the MessageClass, save the item, get its EntryID, and then
close the Inspector and dereference all objects. Then use
Namespace.GetItemFromID to open the item, hopefully displaying the custom
form. Because of Outlook's caching and .NET's garbage collection, I'm not
sure it will work, though.
 
I am following one of your suggestions (using newinspector, changing
the messageclass, saving the item, getting its entryID, closing the
inspector, then using GetItemFromID to open the item), however I
encounter the error "Cannot Display Item" when I call item.Display();
(getItemFromID does not error, but seems to return a blank item) The
item in question, does have its MessageClass changed, and if I manually
open the item after my code has run, the item opens in my custom form.
When the following code is run, the messageclass is successfully
changed, however an error occurs when trying to run item.Display(false)
(the error is "Cannot Display Item") AND the inspector referenced in
"NewInspector" displays anyway. Any ideas of what I am doing wrong?

private void OnNewInspector(Microsoft.Office.Interop.Outlook.Inspector
oInspect)
{
Microsoft.Office.Interop.Outlook.ContactItem oCont =
(Microsoft.Office.Interop.Outlook.ContactItem)oInspect.CurrentItem;
oCont.MessageClass = "IPM.Contact.Test1";
oCont.Save();
string entryID = oCont.EntryID;
oCont.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oCont);

oInspect.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);

Microsoft.Office.Interop.Outlook.ContactItem newItem = null;
newItem =
(Microsoft.Office.Interop.Outlook.ContactItem)applicationObject.GetNamespace("MAPI").GetItemFromID(entryID,
System.Type.Missing);
newItem.Display(false);
}

The item that I am opeing for testing starts as a normal contact
(IPM.Contact)

I have done a couple of variations of GetItemFromID:
object storeID;
Microsoft.Office.Interop.Outlook.MAPIFolder oParent;
oParent = (Microsoft.Office.Interop.Outlook.MAPIFolder)oCont.Parent;
storeID = oParent.StoreID;
....
newItem =
(Microsoft.Office.Interop.Outlook.ContactItem)applicationObject.GetNamespace("MAPI").GetItemFromID(entryID,
storeID);
newItem.Display(false);
//this gives the same "Cannot Display Item" error message
Thank you for any help you can provide.
Kelly Johnson
 
I'm not that familiar with C#, but don't you also need to use:

oCont = null;

to finish the process of removing all references to oCont before you invoke
GetItemFromID?
 
I changed the following lines from:

oCont.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oCont);
oInspect.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
Microsoft.Office.Interop.Outlook.ContactItem newItem = null;

To:

oCont.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oCont);
oInspect.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oInspect);
oCont = null;
oInspect = null;
Microsoft.Office.Interop.Outlook.ContactItem newItem = null;

This did not change the observed behavior. getItemFromID does not
error (but seems to return a blank item, like a shell, if I try to get
any of the properties of the object from the debugger, I get "xxxx
property does not exist", where xxxx is whatever property I tried to
get.)

My code seems to be getting correct values for EntryID and StoreID
(using Outlook spy to compare values).

This is interesting and is at least partially relevant. I change my
custom form (IPM.Contact.Test1) to display a message box when it
closes. the message box doesn't say or do anything important, it just
says "closing". What is interesting, is that when
"oInspect.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);"
is hit in my code, the messagebox I just added to the form is fired.
This indicates to me that the form is closing. However, when my code
has completed running, the form ends up being displayed. (the form
associated with the inspector referenced by "onNewInspector", not the
form I am trying to open with "getItemFromID and Display", which errors)
 
Back
Top