Programmatically Adding Tasks to a MS Project File

B

BenMI

Hi,

I am writing a C#.net application and would like to use C# to add tasks to a
Microsoft Project file. I am not using a VBA macro. I would like to write a
C# function in the .net environment that will open a MS Project file, insert
some tasks, and then close the file.

Could someone please point me in the right direction to get started? Thanks!
 
R

Rod Gill

Firstly, it will be quicker and easier to understand in VB.Net so consider a
VB module.

In Project record a macro of you opening a project and inserting tasks. The
recorded code will give you a start. Note that one of VB's advantages is
that you don't need to specify optional parameters whilst in C# you have to
specify every parameter.

If the project is saved in a database then look at the pjdb.htm file that's
installed in one of Project's program folders and add the tasks to the
database.
 
D

Dhakshna M

Hi,

Try this code

using Microsoft.Office.Interop;

private void btnOpen_Click(object sender, System.EventArgs e)
{
Microsoft.Office.Interop.MSProject.ApplicationClass projectApp = new
Microsoft.Office.Interop.MSProject.ApplicationClass();
projectApp.AppMaximize();
projectApp.FileNew(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.MSProject.Project project =
projectApp.ActiveProject;

Microsoft.Office.Interop.MSProject.Task task;
string t1,t2;
for(int i=0; i<10; i++)
{
task = project.Tasks.Add("Task" + i.ToString(), i+1);
t1 = "10/12/2005";
t2 = "12/12/2005";
task.Duration = DateDiff(Convert.ToDateTime(t2), Convert.ToDateTime(t1));
task.Start = t1;
task.Finish = t2;
task.Text1 = "Task" + i.ToString();
}
}

private double DateDiff(System.DateTime startDate, System.DateTime endDate)
{
double diff=0;
System.TimeSpan TS = new System.TimeSpan(startDate.Ticks-endDate.Ticks);
diff = Convert.ToDouble(TS.TotalDays);
return diff;
}

Regards
Dhakshna
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top