Iterating Properties to Retrieve Values from ActiveProject with C#

K

KenWilson

I am trying to iterate through the properties of the ActiveProject. I'm
using PIA provided by Microsoft. I can get an Application instance and
retrieve the ActiveProject. I can even iterate through the PropertyInfo[]
for the property names but I cannot access the property values. When I try I
am told that my variable that holds the ProjectClass is not an object. What
I want to do is load a Dictionary object with the property names and values
as key/value pairs. Code snippets are here:

private ApplicationClass theApp;
private ProjectClass theProject;

....

public Dictionary<string, string> GetActiveProjectData()
{
if (theProject == null)
{
throw new Exception("No project has been loaded.\n"
+ "Please open a project.");
}

Type type = theProject.GetType();
Dictionary<string, string> dictionary = new Dictionary<string, string>();

foreach (PropertyInfo property in type.GetProperties())
{
dictionary.Add(property.Name,
property.GetValue(theProject, null).ToString());
}

public void OpenFile(....)
{
...

theProject = (ProjectClass) theApp.ActiveProject;

...
}
 
R

Rod Gill

What properties are you looking for? If it's the Title and Subject etc, in
VBA I would use:

activeproject.BuiltinDocumentProperties("Subject")
 
K

KenWilson

Which properties I will need are as yet to be determined. I am currently
trying to find a way to iterate through them using C# code. We are hooking
into the COM object using the C# Primary Interop Assy. From what I've been
able to determine so far the BuiltinDocumentProperties method is not even
available to me. I am finding documentation on using C# with MS Project is
scarce if existent at all. This isn't a Project Server client but a
standalone that my employer wants to be able to do some things in an
automated fashion.
--
Ken Wilson
Coding, Coding over the bounding main()


Rod Gill said:
What properties are you looking for? If it's the Title and Subject etc, in
VBA I would use:

activeproject.BuiltinDocumentProperties("Subject")

--

Rod Gill
Project MVP
Visit www.msproject-systems.com for Project Companion Tools and more


KenWilson said:
I am trying to iterate through the properties of the ActiveProject. I'm
using PIA provided by Microsoft. I can get an Application instance and
retrieve the ActiveProject. I can even iterate through the PropertyInfo[]
for the property names but I cannot access the property values. When I
try I
am told that my variable that holds the ProjectClass is not an object.
What
I want to do is load a Dictionary object with the property names and
values
as key/value pairs. Code snippets are here:

private ApplicationClass theApp;
private ProjectClass theProject;

...

public Dictionary<string, string> GetActiveProjectData()
{
if (theProject == null)
{
throw new Exception("No project has been loaded.\n"
+ "Please open a project.");
}

Type type = theProject.GetType();
Dictionary<string, string> dictionary = new Dictionary<string,
string>();

foreach (PropertyInfo property in type.GetProperties())
{
dictionary.Add(property.Name,
property.GetValue(theProject, null).ToString());
}

public void OpenFile(....)
{
...

theProject = (ProjectClass) theApp.ActiveProject;

...
}
 
R

Rod Gill

Yep, and Visual Basic is a much faster development tool when working with
any office program and the resulting code runs at exactly the same speed as
C# code.

An alternative to having users save project level data in properties,
consider saving the data in custom fields against the project summary task.
In VBA the following code reads project level custom data:

MyProperty=ActiveProject.ProjectSummaryTask.Text1

--

Rod Gill
Project MVP
Visit www.msproject-systems.com for Project Companion Tools and more


KenWilson said:
Which properties I will need are as yet to be determined. I am currently
trying to find a way to iterate through them using C# code. We are
hooking
into the COM object using the C# Primary Interop Assy. From what I've
been
able to determine so far the BuiltinDocumentProperties method is not even
available to me. I am finding documentation on using C# with MS Project
is
scarce if existent at all. This isn't a Project Server client but a
standalone that my employer wants to be able to do some things in an
automated fashion.
--
Ken Wilson
Coding, Coding over the bounding main()


Rod Gill said:
What properties are you looking for? If it's the Title and Subject etc,
in
VBA I would use:

activeproject.BuiltinDocumentProperties("Subject")

--

Rod Gill
Project MVP
Visit www.msproject-systems.com for Project Companion Tools and more


KenWilson said:
I am trying to iterate through the properties of the ActiveProject. I'm
using PIA provided by Microsoft. I can get an Application instance and
retrieve the ActiveProject. I can even iterate through the
PropertyInfo[]
for the property names but I cannot access the property values. When I
try I
am told that my variable that holds the ProjectClass is not an object.
What
I want to do is load a Dictionary object with the property names and
values
as key/value pairs. Code snippets are here:

private ApplicationClass theApp;
private ProjectClass theProject;

...

public Dictionary<string, string> GetActiveProjectData()
{
if (theProject == null)
{
throw new Exception("No project has been loaded.\n"
+ "Please open a project.");
}

Type type = theProject.GetType();
Dictionary<string, string> dictionary = new Dictionary<string,
string>();

foreach (PropertyInfo property in type.GetProperties())
{
dictionary.Add(property.Name,
property.GetValue(theProject,
null).ToString());
}

public void OpenFile(....)
{
...

theProject = (ProjectClass) theApp.ActiveProject;

...
}
 
K

KenWilson

Unfortunately my employer at this moment does not want mixed languages on
this project so C# is, for lack of a better term, one of the constraints I am
to deal with. Thanks for your help though. I'll continue my search.
 
L

Lars Hammarberg

Lots of methods and properties in Project are in fact methods and properties
of the Office object, with Project creating wrappers round them - the
Commandbars object for instance - the BuiltinDocumentProperties is another
Office object.
Create a reference to Office as well and see if you can get things running
better.

/Lars Hammarberg
www.camako.se
 
K

KenWilson

Thank you to both Rod Gill and Lars Hammarberg. Between both of your answers
I am now able to move forward.
 

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