Hi, I am trying to automate MS Project 2007 using C++ (with or without MFC). I have searched the web and found many examples of how to automate Excel and Word, but none for Project, so I am not sure if I am doing it right. When I execute the code below, project opens and is visible, but when I query the tasks on the project, I get an exception. This code doesn't show it, but I have also tried opening an existing file that has tasks and I get the same result. Please let me know if you see anything wrong:
At the time of running this app, I have MS project open on my desktop with a project that has three tasks in it.
The code below imports the OLB, then tries to access the number of tasks, or it tries to retrieve one of the tasks. Both operations result in an error.
I posted this on another message board (MSDN) and someone said that this was because I was using __cdecl calling convention and since MS Project automation uses COM, I need to use stdcall. But I have switched my project to using stdcall and it has made no difference. Besides, the actual code to call the COM interface is in a file called msprj.tli which is automatically generated by Visual Studio when I import the OLB, so I would think any calling convention requirements would be handled there.
Any ideas? I am quite stuck on this. Incidentally, I tried automating Excel and that works just fine, so I don't think it has to do with some problem with office on my machine.
#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\mso.dll" rename("RGB", "MSRGB")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" raw_interfaces_only, rename("Reference", "ignorethis"), rename("VBE", "testVBE")
#import "C:\Program Files\Microsoft Office\OFFICE12\MSPRJ.OLB" auto_search exclude("IFont", "IPicture") rename("RGB", "ignorethis"), rename("DialogBox", "ignorethis"), rename("VBE", "testVBE"), rename("ReplaceText", "EReplaceText"), rename("CopyFile","ECopyFile"), rename("FindText", "EFindText"), rename("NoPrompt", "ENoPrompt")
//Here is the code to automate Project:
MSProject::_MSProjectPtr app(__uuidof(MSProject::Application)); //project opens when this line is called if it wasn't open already
app->PutVisible(-1); //prj is already visible, but if I set this to 0, it hides, it so I am successfully talking to prj
project = app->GetActiveProject(); //project is nonzero after this call
long x = project->GetNumberOfTasks(); //this returns 3 which is correct!!
tasks = project->GetTasks(); //tasks is nonzero after this call
x = tasks->Count; //exception is thrown here. I have also tried tasks->GetCount() and tasks->get_Count(&x) All of which result in the same error
//Also, this produces an error as well:
//task = tasks->GetItem(0); //Error: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call