How to operate microsoft project through vba

Sometimes we need to manipulate MS Project through programs (our company once needed to import the tasks of the company's existing project management platform into MS Project). The project is similar to other Microsoft office software, Word and Excel, and can be operated by VBA.

The following is a general class I wrote in VB. Ne calls VBA to manipulate the MS project.

This class implements common functions such as creating and opening project files, adding tasks, upgrading and downgrading tasks, and saving projects.

Please refer to the notes for details.

Public projects

Author: Sun 04 120 1

Dimmprjapp as MSProject. "Application" refers to the msproject class library in VB.Net.

"Dim mRow As Integer = 0" records the line numbers of tasks in the project.

Public sub-new ()

MPrjApp = new MSProject. application end Sub public Sub new project()

MPrjApp。 FileNew ()' Create a new project file terminator' project can be saved as a database. If saved as a database, we can open this project file directly through ODBC.

Of course, if you are familiar with database projects, you can also directly manipulate the data in the database to modify the project files. Although Microsoft has provided a description of the database structure, it is not very detailed, and there are relatively few official materials in this regard. I will write an article about manipulating the project database through ODBC later.

Public Sub Open(ByVal ODBCName is a string, ByVal ProjectName is a string)

mPrjApp。 File open ("<+odbc name+"> \ "+projectname) end sub public sub new project (byvaltmpfilename as a string)

dim template name As Object = tmpFileName

mPrjApp。 FileNew(False,template name)End Sub public Sub save As(ByVal file path As String)

If io. File exists (file path)

IO。 file . Delete(file path)End IfmPrjApp。 Save the file as (FilePath, MSProject. Pjfileformat.pjmpp) end sub public sub add task (byval taskname is a string, ByVal StartDate is a string, ByVal FinishDate is a string, and optional ByVal Resource is String = "").

MPrjApp。 Select the task field (mRow, name).

MPrjApp。 SetTaskField ("name", task name)

MPrjApp。 SetTaskField ("start time", start date)

MPrjApp。 SetTaskField ("completion date")

MPrjApp。 SetTaskField ("Resource name", resource)' The resource here indicates who the task is assigned to, mrow =1end sub public sub degradation ().

mPrjApp。 outline indent( 1)End Sub public Sub Upgrade()

mPrjApp。 outline outent( 1)End Sub public Sub Close()

mPrjApp。 FileCloseAll(MSProject。 PjSaveType.pjDoNotSave)

MPrjApp。 Quit()End SubEnd Class Example of this class (C#):

Project prj = new Project();

prj。 new project(" D:\ Test \ Test . MPP ");