COMException on x64 platforms when automating the Project client via the Primary Interop Assembly (PIA)

***UPDATE*** Hotfix now available – http://blogs.msdn.com/brismith/archive/2008/11/18/now-fixed-comexception-on-x64-platforms-when-automating-office-clients-via-the-primary-interop-assembly-pia.aspx  

You may see this error when using the CodePlex Test Data Population sample for creating project data using the WinProj tab, or just using your own code to automate winproj.exe (the Microsoft Office Project Professional 2007 client application).  It is only a problem with the object model interaction and not an issue with PSI calls.  It is the Tasks.Add() method which is the trigger for the problem, and it will work just fine on x86, but fails on x64.

Currently the x64 platforms do not support more than 1024 methods on an object (which comes down to around 1017 once the COM standard methods are deducted) and the Tasks object has a lot of methods.

The error is:

Error HRESULT E_FAIL has been returned from a call to a COM component. System.Collections.ListDictionaryInternal.

One work around we have found is re-writing to avoid using the Tasks method.  So the following code:

protected void Page_Load(object sender, EventArgs e)
{
string filename = “c:test.mpp”;
ApplicationClass a = new ApplicationClass();
a.FileNew(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.MSProject.Project p = a.ActiveProject;
 p.Tasks.Add(“test”, Type.Missing);
p.SaveAs(filename, PjFileFormat.pjMPP, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, “MSProject.mpp.9”, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
a.Quit(PjSaveType.pjDoNotSave);
lblfilename.Text = filename;
}

could be re-written as:

protected void Page_Load(object sender, EventArgs e)
{
MSProject.Application objAppProject;
MSProject.Project objProject;
string filename = “c:test.mpp”;
objAppProject = new Microsoft.Office.Interop.MSProject.Application();
objAppProject.FileNew(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
objAppProject.EditGoTo(1, Type.Missing);
objAppProject.SetTaskField(“Name”, “Test”, true, Type.Missing, 1, Type.Missing);
objProject = objAppProject.ActiveProject;
objProject.SaveAs(filename, MSProject.PjFileFormat.pjMPP, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, “MSProject.mpp.9”, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
objAppProject.Quit(MSProject.PjSaveType.pjDoNotSave);
lblfilename.Text = filename;
}

and would work in both x86 and x64 environments.

This is currently being worked on by the Windows teams and we are anticipating a fix.