Access violation in MS Project automation from C++

  • Thread starter Thread starter Jeff Anderson
  • Start date Start date
J

Jeff Anderson

Guest
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.

Importing the olb:


#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")


The actual automation call:

CoInitialize(NULL);

MSProject::_MSProjectPtr app(__uuidof(MSProject::Application)); //project opens when this line is called if it wasn't open already
MSProject::_IProjectDocPtr project;
MSProject::TasksPtr tasks;
MSProject::TaskPtr task;

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



The error is:
Unhandled exception at 0x7712d798 in PRJTest.exe: 0xC0000005: Access violation writing location 0x34379068.

If I step into that line of code, I go to MSProject::Tasks::GetCount() which is in msprj.tli which is generated by the olb import. That function looks like:

inline long MSProject::Tasks::GetCount ( ) {

long _result = 0;
HRESULT _hr = get_Count(&_result); //ERROR HERE
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _result;
}



Does anyone have an idea why the GetNumberOfTasks works, but the tasks->GetCount() does not? The same thing happens if I try to do this with resources instead of tasks (GetResources and GetNumberOfResources), so it isn't unique to tasks...

Thank you very much in advance, I have wasted a lot of time on this problem...

Jeff

Continue reading...
 
Back
Top