NOTE: This page has been translated automatically from Russian to English. Original page.



Upload data in MS Project via COM-connection

Work with Microsoft Project through the COM-connection

Inspired by post .

The lack of material to work with Project'om through COM prompted me to write this article. The material is not intended to be complete, but hopefully will help programmers to some basic points. We used the Russian version of the product, so the names of fields and tables are shown as they were prescribed in VBA when recording macros. For another language, I think, to translate the problems will not be.

Thus, the creation of COMObekta:

MSProject = New COMObekt ( "MSProject.Application");

create a new file:

MSProject. FileNew ();

Gets a collection of tasks:

Tasks = MSProject. ActiveProject. Tasks;

off error and dialog boxes (like "This file already exists") Posts:

MSProject. DisplayAlerts = False;

indicate how many hours per working day:

MSProject. ActiveProject. HoursPerDay = 8;

to add columns to the workspace, use the method in TableEditEx Application:

Table name = "& Enter";
PozitsiyaKolonki = 2;
TipPolya = "Text1";
MSProject. Application. TableEditEx (Table name, True The ,,,,, TipPolya, "Column View" ,,,,,,, ,,,,, PozitsiyaKolonki);

In fact, the table stores a fixed number of columns, and we can only control the visibility of the properties and change. Therefore, we can register any column in its value, even if it is not visible on the sheet. To display the data, you need to add a column with the data type, which we have indicated. The second parameter should be True. List of table names can be seen by running a macro with a single word Tables (and may have a "normal" call this list, but I was not engaged in the search for these). Thereafter, in the form of a list of all available tables of the project.

After adding the columns you need to update the table:

MSProject. Application. TableApply (Table name);

Add a task to a project:

NewTask = Tasks. Add ();
mSootvetstvieZadach. Paste (Zadacha1S, NewTask);

Variable mSootvetstvieZadach this correspondence, for the subsequent addition of precursors, if they are.

Change task level in the hierarchy of tasks (actions similar to pressing the arrow "Demote the problem" and "raise the level of the problem" in the Project panel:

Task. OutlineIndent (); // Lowering
Task. OutlineOutdent (); // level up

Edit fields that can not directly ReadOnly:

NewTask. Name = "Name";
NewTask. Milestone = True; // Milestone

Please note that the fields Start (Start of the problem) and Finish (End task) should not be filled with a blank date:

If ZnachenieZapolneno (DataNachalaPlan) Then
NewTask. Start = DataNachalaPlan + Hour * 9;
ENDIF;
If
ZnachenieZapolneno (DataOkonchaniyaPlan) Then
NewTask. Finish = DataOkonchaniyaPlan + Hour * 18;
ENDIF;

Other fields can be edited via SetField method that is able to work through a constant field names and ID (you can see all the Help ID of all available fields). Example by ID:

NewTask. SetField (188743731, KodSDR); // Text1

Add resource through constant name field. The square brackets indicate Loading resource:

FieldRes = Tasks. Application. FieldNameToFieldConstant ( "Resource Names");
NewTask. SetField (FieldRes, Artist Name + "[80%].");

If the resource is more than one, they are listed in one line, separated by semicolons.

Add precursors by LinkPredecessors method. This is where we need the matching tasks and objectives Project'a 1C:

LinkTask = mSootvetstvieZadach [ZadachaPredshestvennik];
Task. LinkPredecessors (LinkTask);

You can change the font:

MSProject. Application. SelectSheet ();
MSProject. Application. Font32Ex ( "Arial", 8, True);

Save and exit:

MSProject. FileSaveAs (PutKFaylu);
MSProject. Application. Quit ();

1C:Enterprise Developer's Community