Ms Project Add-in Development

Hello Readers !

In this blog, I’m gonna discuss on developing an MS Project Add-in with multiple event handlers like NewProject, ProjectBeforeSave, ProjectAfterSave and ProjectBeforePublish events.

steps:

  1. Open visual studio, File->New->Project->Select Project 2013 and 2016 VSTO Add-in under Office/SharePoint category.

2. Below folder structure will be created.

3. Open ThisAddin.Cs file, and write the below code under the ThisAddIn_Startup function. Define all the event handlers in this function. Then implement all your function definitions. Here I’ve written basic events to illustrate the events for the the demo purpose.

Application_NewProject: will fire every time when we create a new project. (NewProjectCreate)

Application_ProjectBeforeSave:  will fire Before saving the Project (ProjectBeforeSave)

Application_ProjectAfterSave : will fire after the project saved. (ProjectSaved event)

ProjectBeforePublish : will fire before publishing the project (PreProjectPublish)


private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewProject += Application_NewProject;
this.Application.ProjectAfterSave += Application_ProjectAfterSave;
this.Application.ProjectBeforeSave += Application_ProjectBeforeSave;
this.Application.ProjectBeforePublish += Application_ProjectBeforePublish;
}

private void Application_ProjectBeforePublish(MSProject.Project pj, ref bool Cancel)
{
/ throw new NotImplementedException();
DialogResult resul = MessageBox.Show("Raj, Do you really want to publish this project.", "Confirmation",            System.Windows.Forms.MessageBoxButtons.YesNo);
if (resul == DialogResult.No)
{
Cancel = true;
MessageBox.Show("You have cancelled the publish");
}

}

private void Application_ProjectAfterSave()
{
// throw new NotImplementedException();
DialogResult res= MessageBox.Show("Raj, Project Saved Scuccessfully.", "Confirmation", System.Windows.Forms.MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
{
MessageBox.Show("clicked Yes");
}
else
{
MessageBox.Show("Bye Raj");
}
}

private void Application_ProjectBeforeSave(MSProject.Project pj, bool SaveAsUi, ref bool Cancel)
{
DialogResult result= System.Windows.Forms.MessageBox.Show("Hello Rajkumar, you are trying to save the project, do you?","Confirmation",System.Windows.Forms.MessageBoxButtons.YesNo);
if(result==DialogResult.No)
{
Cancel = true;
MessageBox.Show("You have cancelled save");
}

}

private void Application_NewProject(MSProject.Project pj)
{
//throw new NotImplementedException();
MSProject.Task newTask = pj.Tasks.Add("This is a new Task, created by VSTO addin", missing);
newTask.Start = DateTime.Today.AddDays(2);
newTask.Notes = "This is a new task note";
newTask.Duration = "10";
newTask.ResourceNames = "Raj,Kumar,Allepu";
System.Windows.Forms.MessageBox.Show("A new Task has been created successfully.");
}

After implementing the code, build the solution and click on F5 (or) Run to test the solution. The solution will publish and launches the MS Project application.

Below are the output screenshots.

  1. Application_NewProject:

2. Application_ProjectBeforeSave

3. Application_ProjectAfterSave

4. Application_ProjectBeforePublish

 

Now, to uninstall the add-in from the MS Project application, Righ-Click on the Solution in Visual studio and click on “Clean” option.

That’s it. Thanks for reading the post.

-Rajkumar Allepu.