Walkthrough: Create your first VSTO Add-in for Project
This walkthrough shows you how to create a VSTO Add-in for Microsoft Office Project. The features that you create in this kind of solution are available to the application itself, regardless of which projects are open. For more information, see Office solutions development overview (VSTO).
Applies to: The information in this topic applies to VSTO Add-in projects for Project. For more information, see Features available by Office application and project type.
This walkthrough illustrates the following tasks:
Creating a Project VSTO Add-in project.
Writing code that uses the object model of Project to add a task to a new project.
Building and running the project to test it.
Cleaning up the completed project so that the VSTO Add-in no longer runs automatically on your development computer.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the IDE.
Prerequisites
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configure a computer to develop Office solutions.
Project 2013 or Project 2010.
Create the project
To create a new project in Visual Studio
Start Visual Studio.
On the File menu, point to New, and then click Project.
In the templates pane, expand Visual C# or Visual Basic, and then expand Office/SharePoint.
Under the expanded Office/SharePoint node, select the Office Add-ins node.
In the list of project templates, select Project 2010 Add-in or Project 2013 Add-in.
In the Name box, type FirstProjectAddIn.
Click OK.
Visual Studio creates the FirstProjectAddIn project and opens the ThisAddIn code file in the editor.
Write code that adds a new task to a project
Next, add code to the ThisAddIn code file. The new code uses the object model of Project to add a new task to a project. By default, the ThisAddIn code file contains the following generated code:
A partial definition of the
ThisAddIn
class. This class provides an entry point for your code and provides access to the object model of Project. For more information, see Program VSTO Add-ins. The remainder of theThisAddIn
class is defined in a hidden code file that you should not modify.The
ThisAddIn_Startup
andThisAddIn_Shutdown
event handlers. These event handlers are called when Project loads and unloads your VSTO Add-in. Use these event handlers to initialize your VSTO Add-in when it is loaded, and to clean up resources used by your VSTO Add-in when it is unloaded. For more information, see Events in Office projects.
To add a task to a new project
In the ThisAddIn code file, add the following code to the
ThisAddIn
class. This code defines an event handler for theNewProject
event of theMicrosoft.Office.Interop.MSProject.Application
class.When the user creates a new project, this event handler adds a task to the project.
void Application_NewProject(Microsoft.Office.Interop.MSProject.Project pj) { MSProject.Task newTask = pj.Tasks.Add ("This text was added by using code", missing); newTask.Start = DateTime.Now; newTask.Duration = "3"; newTask.ResourceNames = "Rob Caron, Kelly Krout"; }
To modify the project, this code example uses the following objects:
The
Application
field of theThisAddIn
class. TheApplication
field returns aMicrosoft.Office.Interop.MSProject.Application
object, which represents the current instance of Project.The
pj
parameter of the event handler for the NewProject event. Thepj
parameter is aMicrosoft.Office.Interop.MSProject.Project
object, which represents the project. For more information, see Project solutions.
If you are using C#, add the following code to the
ThisAddIn_Startup
event handler. This code connects theApplication_Newproject
event handler with the NewProject event.this.Application.NewProject += new Microsoft.Office.Interop.MSProject._EProjectApp2_NewProjectEventHandler(Application_NewProject);
Test the project
When you build and run the project, verify that the new task appears in the resulting new project.
To test the project
Press F5 to build and run your project. Microsoft Project starts and automatically opens a new blank project.
When you build the project, the code is compiled into an assembly that is included in the build output folder for the project. Visual Studio also creates a set of registry entries that enable Project to discover and load the VSTO Add-in, and it configures the security settings on the development computer to enable the VSTO Add-in to run. For more information, see Office solution build process overview.
Verify that a new task is added to the blank project.
Verify that the following text appears in the Task Name field of the task.
This text was added by using code.
Close Microsoft Project.
Clean up the project
When you finish developing a project, remove the VSTO Add-in assembly, registry entries, and security settings from your development computer. Otherwise, the VSTO Add-in will run every time you open Microsoft Project on the development computer.
To clean up your project
- In Visual Studio, on the Build menu, click Clean Solution.
Next steps
Now that you have created a basic VSTO Add-in for Project, you can learn more about how to develop VSTO Add-ins from these topics:
General programming tasks that you can perform in VSTO Add-ins for Project: Program VSTO Add-ins.
Using the object model of Project: Project solutions.
Building and debugging VSTO Add-ins for Project: Build Office solutions.
Deploying VSTO Add-ins for Project: Deploy an Office solution.