Excel 2007 C# CustomDocumentProperties

S

spraycan77

I created an add-in for Excel 2007 in C# (.NET 3.5). I am trying to
get access to the Custom Document Properties and when I try and
retrieve the collection using the below code I keep getting a null
value for the collection.

Microsoft.Office.Interop.Excel.CustomProperties properties =
app.ActiveWorkbook.CustomDocumentProperties as
Microsoft.Office.Interop.Excel.CustomProperties;

I tried adding a custom string field in the Excel UI before running my
add in, but I still get a null value.

Is there something that is restricting me from getting to this data?

Thanks,
Jason
 
J

Jialiang Ge [MSFT]

Good morning Jason. I'm Jialiang from Microsoft Newsgroup support team.
Welcome to office com add-in newsgroup! It is my pleasure to work with you
on this issue!

PROBLEM
==============
The code line "app.ActiveWorkbook.CustomDocumentProperties as
Microsoft.Office.Interop.Excel.CustomProperties; " always returns null when
there are indeed some custom document properties in the workbook.

CAUSE
==============
This is a known code defect of Office PIA:
app.ActiveWorkbook.CustomDocumentProperties fails to be casted to the
CustomProperties type, and returns "null" from the code line. To see the
error in the cast of the type, we change the code as this:

Microsoft.Office.Interop.Excel.CustomProperties properties =
(Microsoft.Office.Interop.Excel.CustomProperties)book.CustomDocumentProperti
es;

And it throws an exception: "Unable to cast COM object of type
'System.__ComObject' to interface type
'Microsoft.Office.Interop.Excel.CustomProperties'. This operation failed
because the QueryInterface call on the COM component for the interface with
IID '{00024452-0000-0000-C000-000000000046}' failed due to the following
error: No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."

Microsoft recognized this issue, and built a KB article to explain the
cause and workarounds:
http://support.microsoft.com/default.aspx?scid=303296
The problem applies to all the office product (Word, Excel, Project, etc)
and all Office versions (XP, 2003, 2007).

RESOLUTION
=============
Late binding the project as per the article:

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

For your code snippet, I translate them to last binding codes as this:
(suppose there's a custom property "MyProp" in the workbook)

object customProperties = app.ActiveWorkbook.CustomDocumentProperties;
Type dpType = customProperties.GetType();
string strIndex = "MyProp";
string strValue;
object oMyProp = dpType.InvokeMember("Item",
System.Reflection.BindingFlags.GetProperty, null, customProperties, new
object[] { strIndex });
Type myPropType = oMyProp.GetType();
strValue = myPropType.InvokeMember("Value",
System.Reflection.BindingFlags.GetProperty, null, oMyProp, new object[] {
}).ToString();

Please feel free to contact me with your questions and concerns, if any and
I will address them for you.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Jason,

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of the suggestions? If you need further
assistance, feel free to let me know. I will be more than happy to be of
assistance.

Have a great day!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

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