27 May 2011

COM Error on PublishForm for Outlook (and you wouldn't believe the solution!)

The problem: an oft file for deployment.
Here is some code in C# which I've copied & pasted into my C# code: (source is in here)

string filename; string location;
_Application olApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
_NameSpace olNs = olApp.GetNamespace("MAPI");
MAPIFolder oInbox = olNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
_MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)olApp.CreateItemFromTemplate(location, oInbox);
Microsoft.Office.Interop.Outlook.FormDescription oFormDesc = (Microsoft.Office.Interop.Outlook.FormDescription)oMailItem.FormDescription;


oFormDesc.Name = filename;
oFormDesc.PublishForm(OlFormRegistry.olFolderRegistry, oInbox);
oMailItem.Close(OlInspectorClose.olDiscard);


And this code would fail on the line

oFormDesc.PublishForm(OlFormRegistry.olFolderRegistry, oInbox);

With an error "COMException was Unhandled"

The reason:
The filename (the parameter given) included the string " - ".
So for filename "Broker forms" - pass,
"Broker - forms" -fail.