Link to home
Start Free TrialLog in
Avatar of jerboa
jerboa

asked on

MSProject98 COM

I have a problem accessing the Tasks in a MSProject98 through the COM interface (msprj98.olb). I can access the property alrite but when I need to get detailed information about the Tasks I get a OLEAUT32.DLL access violation
Avatar of Madshi
Madshi

I don't now the MSProject98 com interface. But if you post some code here, perhaps I'll find a bug in your sources...

Regards, Madshi.
Avatar of jerboa

ASKER

{Import the 'msprj8.olb' type library to compile}
type
 TProject98 = class  
   private
     FProject98: _MsProject;

constructor TProject98.Create;
begin
  FProject98 := CoApplication_.Create;
end;

function TProject98.GetNumberOfTasks: Integer;
begin
  {This Code crashes}
  Result := FProject98.ActiveProject.Tasks.Count;
end;

function TProject98.GetTasks: integer;
var
  oTasks: Tasks;
begin
  oTasks := FProject98.ActiveProject.Tasks; {This works}
  Result := oTasks.Count; {This crashes too}
end;

Avatar of jerboa

ASKER

{Import the 'msprj8.olb' type library to compile}
type
 TProject98 = class  
   private
     FProject98: _MsProject;

constructor TProject98.Create;
begin
  FProject98 := CoApplication_.Create;
end;

function TProject98.GetNumberOfTasks: Integer;
begin
  {This Code crashes}
  Result := FProject98.ActiveProject.Tasks.Count;
end;

function TProject98.GetTasks: integer;
var
  oTasks: Tasks;
begin
  oTasks := FProject98.ActiveProject.Tasks; {This works}
  Result := oTasks.Count; {This crashes too}
end;

Hi Jerboa,

Had an evaluation version somewhere on my disk, so did the install to get to the typelib. If the code above is all you have so far then I will explain the errors.

crash 1 is because you have no Activeproject open
crash 2 same reason.

This will get you on your way

procedure TForm1.Button1Click(Sender: TObject);
var
  aProject: OleVariant;
  CurrentTasks: Integer;
begin
  aProject := CreateOleObject('MSProject.Application');
  aProject.Visible := True;
  try
    aProject.FileNew;
    CurrentTasks := aProject.ActiveProject.NumberOfTasks;
    Showmessage(IntToStr(CurrentTasks));
  except
    aProject.Free;//project doesn't support free but the object will be released in case of trouble
  end;
end;


But the beauty (and the trouble) is that all this MS stuff is going through OleVariants. So before you can acces the information you would like to see, you have to create an instance of the application and put it into an Olevariant.
Then you have to go through the object model of Project to acces what you want to have.

If you have any questions about this ask them.
br(UINT)je.
Avatar of jerboa

ASKER

I had an active project open and the NumOfTasks property worked fine but I need to get the more detailed info that the Tasks interface provides. NumOfTasks is a property of the ActiveProject (works fine in my code too) but I need some code to access the names of each tasks (Name is a property of the Task interface), I get an error whenever I try to access any property of the Tasks interface:

procedure TProject98.GetTaskNames;
var
  oTasks: Tasks;
  FTasks: TStringList;
begin
  FTasks := TStringlist.Create;
  try
    oTasks := FProject98.ActiveProject.Tasks; {This works}
    for iCounter := 1 to FProject98.ActiveProject.NumOfTasks do
      FTasks.Lines.Add := oTasks.Item[iCounter].Name; {This
            code compiles but crashes at runtime}
  finally
     FTasks.Free;
  end;
end;
How does the Tasks type look like?
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and of course things will compile just fine I suspect that delphi doesn't check all these OLE properties to good(correct me if I am wrong.)


Avatar of jerboa

ASKER

Thanky Bruintje,

Ik heb de OLE Variable van type verandert
    _MSProject -> OLEVariant en nu kan ik wel aan de Task informatie. Dan verlies ik wel een beetje Early binding functionaliteit omdat de compiler niet echt kan type checken. Maar de code werkt en dat is het belangrijkste...