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

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

Lars Hammarberg

The following goes for "old-style" non-managed code coding and is perhaps
applicable in this scenario as well:
The BuiltinDocumentProperties is actually a part of the Microsoft.Office
classes and is wrapped in the Project object libs - I wonder if you'd get a
better result if you add "using Microsoft.Office.Interop" or whatever the
interop is called for the "basic" Office libs to those already entered in
your code.

/Lars Hammarberg
www.camako.se
 
K

KenWilson

I checked using the object browser and BuiltInDocumentProperties only appears
in Project PIA, nowhere else. When I code this function with
project.BuiltInDocumentProperties it only offers four basic functions beyond
that, one of which is ToString(). ToString is useless as all it returns is
"System.__ComObject" in every case.
 
P

PeterNZ

It works for me. But the difference is, I don't create an object
"ApplicationClass". Use "MSProj.Application" instead. You then do
"app.FileOpen(...)". Replace all Optional parameters which you don.t use with
The next step is to assign the "ActiveProject" Property to your Project
Object like "MSProject.Project project = app.ActiveProject;". You can then
work with your project Object. BTW, I don't see where you call your OpenFile
function.

HTH

Peter
 
K

KenWilson

I tried declaring it as MSProj.Application as you suggested and it makes no
difference. My method for getting the active project already is, as you
suggested,
MSProj.Project project = app.ActiveProject [see public void OpenFile(string
filename)]. Would it make a difference that I am using .NET 2.0 and VS 2005?
 
P

PeterNZ

Sorry can't answer the questions re Framework 2.0 and VS 2005. I am not using
this for production yet. But honestly, it would surprise me if it would make
a difference. On the other side, life is full of surprises.

It might be stupid question, but have you checked the return value of your
"app.FileOpen(...)" call? Does it return "true"?

Cheers

Peter

KenWilson said:
I tried declaring it as MSProj.Application as you suggested and it makes no
difference. My method for getting the active project already is, as you
suggested,
MSProj.Project project = app.ActiveProject [see public void OpenFile(string
filename)]. Would it make a difference that I am using .NET 2.0 and VS 2005?
--
Ken Wilson
Coding, Coding over the bounding main()


PeterNZ said:
It works for me. But the difference is, I don't create an object
"ApplicationClass". Use "MSProj.Application" instead. You then do
"app.FileOpen(...)". Replace all Optional parameters which you don.t use with
The next step is to assign the "ActiveProject" Property to your Project
Object like "MSProject.Project project = app.ActiveProject;". You can then
work with your project Object. BTW, I don't see where you call your OpenFile
function.

HTH

Peter
 
P

PeterNZ

Sorry, just another piece of information,. The only difference I can see
between your FileOpen and my call is, that I use Missing.Value for the
ReadOnly Parameter as well. I am not sure what the default value is, since
the msdn help doesn't say it.

But this shouldn't make a difference, should it?

Cheers

Peter

KenWilson said:
I tried declaring it as MSProj.Application as you suggested and it makes no
difference. My method for getting the active project already is, as you
suggested,
MSProj.Project project = app.ActiveProject [see public void OpenFile(string
filename)]. Would it make a difference that I am using .NET 2.0 and VS 2005?
--
Ken Wilson
Coding, Coding over the bounding main()


PeterNZ said:
It works for me. But the difference is, I don't create an object
"ApplicationClass". Use "MSProj.Application" instead. You then do
"app.FileOpen(...)". Replace all Optional parameters which you don.t use with
The next step is to assign the "ActiveProject" Property to your Project
Object like "MSProject.Project project = app.ActiveProject;". You can then
work with your project Object. BTW, I don't see where you call your OpenFile
function.

HTH

Peter
 
K

KenWilson

I'll answer both questions at once. :) My call to app.FileOpen(...) returns
true. As for the ReadOnly it doesn't matter. At this point I don't want to
be writing anyway until I have a better handle on issues. That's if I ever
get over this hurdle.

As for Microsoft help not saying anything I find that tends to be SOP with
them.
--
Ken Wilson
Coding, Coding over the bounding main()


PeterNZ said:
Sorry, just another piece of information,. The only difference I can see
between your FileOpen and my call is, that I use Missing.Value for the
ReadOnly Parameter as well. I am not sure what the default value is, since
the msdn help doesn't say it.

But this shouldn't make a difference, should it?

Cheers

Peter

KenWilson said:
I tried declaring it as MSProj.Application as you suggested and it makes no
difference. My method for getting the active project already is, as you
suggested,
MSProj.Project project = app.ActiveProject [see public void OpenFile(string
filename)]. Would it make a difference that I am using .NET 2.0 and VS 2005?
--
Ken Wilson
Coding, Coding over the bounding main()


PeterNZ said:
It works for me. But the difference is, I don't create an object
"ApplicationClass". Use "MSProj.Application" instead. You then do
"app.FileOpen(...)". Replace all Optional parameters which you don.t use with
The next step is to assign the "ActiveProject" Property to your Project
Object like "MSProject.Project project = app.ActiveProject;". You can then
work with your project Object. BTW, I don't see where you call your OpenFile
function.

HTH

Peter

:

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

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