MS Project COM Interop - Project is not an Object - C#

K

Ken Wilson

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. 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. 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);
}
}
}

Ken Wilson
Seeking viable employment in Victoria, BC
 
G

Gary L. Chefetz [MVP]

Ken:

You should ask developer questions at microsoft.public.project.developer.
 
K

Ken Wilson

Ken:

You should ask developer questions at microsoft.public.project.developer.

Thank you. I will do that. I will have to use Google as my ISP
doesn't carry that newsgroup.

Ken Wilson
Seeking viable employment in Victoria, BC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top