Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I have been searching online for some sample codes on how to correctly load and save a Microsoft Project file. After digging and some trials and errors, I stumbled upon the problem of saving the same MPP file I opened. I tried playing with the attributes, but to no avail. Here are the codes I used to play with:
object fileName = "C:\\Test.mpp";
object readOnly = false;
object missing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.MSProject.PjMergeType merge = Microsoft.Office.Interop.MSProject.PjMergeType.pjDoNotMerge;

Microsoft.Office.Interop.MSProject.PjPoolOpen pool = Microsoft.Office.Interop.MSProject.PjPoolOpen.pjDoNotOpenPool;

object ignoreReadOnlyRecommended = false;
            
Microsoft.Office.Interop.MSProject.ApplicationClass appclass = new Microsoft.Office.Interop.MSProject.ApplicationClass();

appclass.FileOpen(fileName, readOnly, merge, missing, missing, missing, missing, missing, missing, missing, missing, pool, missing, missing, ignoreReadOnlyRecommended, missing);

appclass.Visible = false;

Microsoft.Office.Interop.MSProject.Project project = appclass.ActiveProject;
            
project.Resources.Add("A", 1);
project.Resources.Add("B", 2);
project.Resources.Add("C", 3);
project.Resources.Add("D", 4);
project.Resources.Add("E", 5);

Microsoft.Office.Interop.MSProject.Task task = project.Tasks.Add("Requirement Collection",missing);

task.Start = "08/1/2007";
task.Finish = "10/25/2007";

task.Assignments.Add(task.ID, 1, missing);
task.Assignments.Add(task.ID, 2, missing);

task.Milestone = true;
task.OutlineLevel = 2;

appclass.FileSave();

The problem occurs when I am trying to save. It opens up Microsoft Project and prompt me to save as instead. I just want it to let Microsoft Project out of it and quietly save. I notice that the file is opened as Read-Only even though I specified it to open not as Read-Only. I figure that I might be doing something wrong, but I have no idea what. Please point me what I am doing wrong.

By the way, I am using Microsoft Project 12.0 Object Library.

Also, how do I correctly attach a predecessor to a task?
Posted
Updated 14-Apr-11 15:19pm
v5
Comments
googoole 11-Apr-13 10:06am    
i need to add new calendar type (such as georgian calender) in setting tab ? can you help me ?
Que Trac 11-Apr-13 16:27pm    
Hi, I am not sure what you mean by adding a new calendar type to the setting tab. The code above is for writing data to a particular Microsoft Project file. It seems to me that your question is not directly related to what is shown above. I suggest you to direct your question to a new, but separate question page.
Ganesh Kumar.G 9-Nov-15 2:01am    
While i am creating
ApplicationClass appclass = new ApplicationClass();
it will show's the following error
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ConsoleApplication1.exe
KalpanaGupta28 14-Apr-17 0:43am    
I am getting error as "appclass doesnot exist in the current context"
appclass.FileOpen(fileName, readOnly, merge, missing, missing, missing, missing, missing, missing, missing, missing, pool, missing, missing, ignoreReadOnlyRecommended, missing);

Works OK for me (in Project 10.0). The only way that I could find to make it come up with the SaveAs dialog was if the source file was read only in the first place
 
Share this answer
 
Comments
Joezer BH 10-Sep-13 3:34am    
5ed!
After searching online and testing out on a different directory, it works. It was Windows 7 administrator control interfering with the program in the first place. Also, after opening the file, I need to close it. If I don't close the file, the project file will stack up in the task-bar.

MIDL
//appclass.FileSave();
appclass.FileClose(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave, missing);
 
Share this answer
 
v3
Comments
pk patel 16-Oct-12 7:40am    
hi. I am also using the same code. But i get following exceptation.
can you help to solve it out???

"Retrieving the COM class factory for component with CLSID {36D27C48-A1E8-11D3-BA55-00C04F72F325} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."
Joezer BH 10-Sep-13 3:35am    
5ed!
Que Trac 11-Apr-13 16:18pm    
I don't recall getting that kind of errors before, but after googling for it, you can try to see if the following link will fix the problem.

http://digioz.blogspot.com/2011/09/retrieving-com-class-factory-for.html
SSV3273 1-Mar-16 6:22am    
Throws an error that the interop class APplicationClass cannot be embedded, try using the interface instead....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900