24 | 05 | 2017

Connect to MS Project

To connect to a Microsoft Project using VB.net take the following steps. 

1. Set the reference to Microsoft.Office.Interop.MSProject

2. Add the following code to VB.net

3. Create a button to test the connection

 

Imports Microsoft.Office.Interop.MSProject

Public Class Form1

    Function TestConnection() As Boolean


        On Error GoTo errorhandler

        Dim pjApplication As New Application


        'Create an mpp file
        pjApplication.FileNew(Template:="")
        pjApplication.Visible = True

        pjApplication.ScreenUpdating = False 'diasble visible screenupdates until the code is completed

        Dim tsks As Tasks
        Dim t As Task

        tsks = pjApplication.ActiveProject.Tasks

        'CREATE A NEW TASK
        t = tsks.Add("TASK 1")
        t.OutlineLevel = 1 'set the task level

        'CREATE A NEW SUB TASK
        t = tsks.Add("SUBTASK 1")
        t.OutlineLevel = 2 'set the task level


        pjApplication.ScreenUpdating = True 'Activate visible screenupdates until the code is completed

        TestConnection = True

        Exit Function
errorhandler:
        'report error
        MsgBox(Err.Description & "-" & Err.Number)
        TestConnection = False
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        MsgBox(TestConnection()) 'returns true if connection succeeded.

    End Sub
End Class

If the code has run successfully it will open a new project and add the lines like below:

Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2258964
Latest Articles