Art  Delphi  Automation  History  Home  Politics  Email me Default Colours  Printable Colours

Automating MSProject

I know nothing about MSProject. However, Bjørge Sæther and Darren Guy have kindly sent me sample code that may help you.

Bjørge Sæther has provided a sample project for MSProject95, which you can download. Thanks, Bjørge! Note that it's a sample only, not compilable. The code uses Binh Ly's ComLib.pas - useful routines for COM that you can download from his site.

Darren Guy has sent this code snippet, which should get you started:

uses MSProject_TLB;
// Global var
MSProject: Variant;

procedure TForm1.OpenProjectClick(Sender: TObject);
try
    // Initialise MSProject variant to be the MSProject Application
    MSProject := CreateOleObject ('MSProject.Application');
    try
      MSProject.Visible := True;
      MSProject.Projects.Add(False);
      MSProject.ActiveProject.Title := 'Testing Here';
    except
      on E: Exception do ShowMessage('Exception Error...' + E.Message);
    end;
  except
    on E: Exception do 
           ShowMessage('MS Project App not found...' + E.Message);
  end;
end;

function TForm1.AddTask;
var
  Name: OleVariant;
begin
  Result := False;
  if not VarIsEmpty(MSProject) then begin
    MSProject.ActiveProject.Tasks.Add('Task Name Here');
    Result := True;
  end;
end;

procedure TForm1.CloseProjectClick(Sender: TObject);
begin
  if not VarIsEmpty(MSProject) then begin
    MSProject.Quit(pjDoNotSave);
    MSProject := NULL;
  end;
end;