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.