DocumentProperties prps = (DocumentProperties)mspApp.ActiveProject

D

DapperDanH

The following code:
======================================
using Microsoft.Office.Core;
DocumentProperties prps =
(DocumentProperties)mspApp.ActiveProject.CustomDocumentProperties;
======================================
works great when the class that contains that code is called as part of a
..NET C# MSProject addin.

However, as a standalone, using the exact same references to MS Project and
Office (.net tab), I receive an invalid cast at runtime. It does compile.

Unfortunately, I do not know how to get around this and have been stuck.
Anyone have any advice?

Sincerely,
Dan H.
 
W

Wei-Dong XU [MSFT]

Hi Dan,

Currently we are researching this issue for you. Greatly appreciate your
patience! If there is any idea, we will update you then.

Best Regards,
Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Ken Laws [MSFT]

Hi Dan,

My name is Ken Laws and I am with the Microsoft Office Integration team.

Upon reviewing the information that you provided, I setup a test with a C#
.NET Shared Add-in and a C# .NET Windows application and added the code the
you supplied. As you had indicated when I tested the code from the Shared
Add-in, the code executed without any errors, however, upon executing the
code from the Windows application I did receive the "Specified cast is not
valid" error when referencing the following line of code.

DocumentProperties prps =
(DocumentProperties)oApp.ActiveProject.CustomDocumentProperties;

Upon researching the issue further, it appears that the error is due to an
issue with marshalling the call across process.

I found a similar case when automating Microsoft Word and attempting to
retrieve information from the CustomDocumentProperties.

In this case they implemented code from the Knowledge Base article 303296
to resolve this problem.

303296: How To Use Automation to Get and to Set Office Document Properties
with Visual C# .NET
http://support.microsoft.com/?id=303296

I hope this information helps!

If you have any questions please let me know via the posting.

Regards,

Ken Laws
Microsoft Support


This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Ken Laws [MSFT]

Hi Dan,

I have included a snippet of code below which demonstrates how to loop
through the custom document properties using the InvokeMember method.

Sample Code
===========================
oProj = oApp.ActiveProject;

object oDocCustomProps = oProj.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();

//Obtain a count of the custom document properties.
int PropCount = (int)typeDocCustomProps.InvokeMember("Count",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocCustomProps,
new object[] {});

//Using the count of custom document properties,
//loop through each property.
for (int i=1;i<=PropCount;i++)
{
//Obtain a reference to the property.
object oDocProp = typeDocCustomProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocCustomProps,
new object[] {i} );

Type tDocProp = oDocProp.GetType();

//Using the oDocProp reference obtain the "Name"
//of the selected custom document property.
string sDocPropName = tDocProp.InvokeMember("Name",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocProp,
new object[] {}).ToString();

//Using the oDocProp reference obtain the "Value"
//of the selected custom document property.
string sDocPropValue = tDocProp.InvokeMember("Value",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocProp,
new object[] {}).ToString();

//Display a message box with the property name and value.
MessageBox.Show("Property - " + sDocPropName + "\n" +
"Value - " + sDocPropValue);
}
===========================

I hope this helps!

If you have any questions please let me know via the posting.

Regards,

Ken Laws
Microsoft Support


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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