K
Ken Wilson
This has been out here once before but I'm still struggling. I thank
you for your patience.
I have been tasked with developing a program to access .mpp Project
files using the MS Project COM object using C#. I am having a problem
getting things off the ground possibly due to some misunderstanding of
the problem domain on my part or missing some critical components at a
basic level. This code compiles successfully.
I have attached the code for the assembly that will model the active
project. The error I am getting is a runtime error in the
GetActiveProjectData() method when I try to add the property name and
its value to the Dictionary object. The runtime error states that
'project' is not an object. I have substituted other objects, i.e.
'app' and the runtime error is then that the object is the wrong
target. Also, the two lines of code that are commented out are the
other attempts I have made to move forward that had the same net
result.
Any assistance or insight would be greatly appreciated. Thank you.
....
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using MSProj = Microsoft.Office.Interop.MSProject;
namespace ns1.ns2.msproject
{
public class MsPModel
{
MSProj.ApplicationClass app;
MSProj.Project project;
public MsPModel()
{
app = new MSProj.ApplicationClass();
}
public Dictionary<string, string> GetActiveProjectData()
{
if (project == null)
{
throw new Exception("No project has been loaded.\n"
+ "Please open a project.");
}
Dictionary<string, string> dictionary =
new Dictionary<string, string>();
Type type = project.GetType();
foreach (PropertyInfo property in type.GetProperties())
{
dictionary.Add(property.Name,
property.GetGetMethod().Invoke(project, null).ToString));
//project.BuiltinDocumentProperties.ToString());
//property.GetValue(project, null).ToString());
}
return dictionary;
}
public void OpenFile(string filename)
{
try
{
app.FileOpen(filename, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value,MSProj.PjPoolOpen.pjPromptPool,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
project = app.ActiveProject;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "MSProject");
return;
}
}
public void Quit()
{
try
{
app.FileClose(MSProj.PjSaveType.pjDoNotSave, false);
}
catch (Exception ex)
{
}
app.Quit(MSProj.PjSaveType.pjDoNotSave);
}
}
}
you for your patience.
I have been tasked with developing a program to access .mpp Project
files using the MS Project COM object using C#. I am having a problem
getting things off the ground possibly due to some misunderstanding of
the problem domain on my part or missing some critical components at a
basic level. This code compiles successfully.
I have attached the code for the assembly that will model the active
project. The error I am getting is a runtime error in the
GetActiveProjectData() method when I try to add the property name and
its value to the Dictionary object. The runtime error states that
'project' is not an object. I have substituted other objects, i.e.
'app' and the runtime error is then that the object is the wrong
target. Also, the two lines of code that are commented out are the
other attempts I have made to move forward that had the same net
result.
Any assistance or insight would be greatly appreciated. Thank you.
....
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using MSProj = Microsoft.Office.Interop.MSProject;
namespace ns1.ns2.msproject
{
public class MsPModel
{
MSProj.ApplicationClass app;
MSProj.Project project;
public MsPModel()
{
app = new MSProj.ApplicationClass();
}
public Dictionary<string, string> GetActiveProjectData()
{
if (project == null)
{
throw new Exception("No project has been loaded.\n"
+ "Please open a project.");
}
Dictionary<string, string> dictionary =
new Dictionary<string, string>();
Type type = project.GetType();
foreach (PropertyInfo property in type.GetProperties())
{
dictionary.Add(property.Name,
property.GetGetMethod().Invoke(project, null).ToString));
//project.BuiltinDocumentProperties.ToString());
//property.GetValue(project, null).ToString());
}
return dictionary;
}
public void OpenFile(string filename)
{
try
{
app.FileOpen(filename, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value,MSProj.PjPoolOpen.pjPromptPool,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
project = app.ActiveProject;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "MSProject");
return;
}
}
public void Quit()
{
try
{
app.FileClose(MSProj.PjSaveType.pjDoNotSave, false);
}
catch (Exception ex)
{
}
app.Quit(MSProj.PjSaveType.pjDoNotSave);
}
}
}