Hi, using Visual C# 2010 Express and MS Project 2007 (and then tested with MS Project 2010 too).
I am not using Visual Studio Tools for Office, but can read/write data to MS Project files using Visual Studio 2010 solution. However, I am unable to retrieve list of microsoft project file CustomDocumentProperties
I have reference:
using MSProject = Microsoft.Office.Interop.MSProject;
and code:
MSProject.Application projApp = new MSProject.Application();
projApp.FileOpen(<mppfile>, true);
MSProject.Project proj = projApp.ActiveProject;
For a known existing CustomDocumentProperty name I can retrieve value as:
string strDBName = proj.CustomDocumentProperties.Item("Databases").Value.ToString();
However, if I want to get list of all existing CustomDocumentProperty names and their values, how can I do that?
Either of the following lines:
Microsoft.Office.Core.DocumentProperties docProps = proj.CustomDocumentProperties;
Microsoft.Office.Core.DocumentProperties docProps = (Microsoft.Office.Core.DocumentProperties)proj.CustomDocumentProperties;
throw exception:
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
The following code:
System.Collections.Generic.List<string> list = proj.CustomDocumentProperties.Items.ToList();
throw exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
'System.__ComObject' does not contain a definition for 'Items'
at CallSite.Target(Closure , CallSite , ComObject )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
The following code for non-existing CustomPropertyName:
string strDBName = proj.CustomDocumentProperties.Item("BadValue").Value.ToString();
throws exception:
System.ArgumentException: Value does not fall within the expected range.
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , ComObject , String )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at CallSite.Target(Closure , CallSite , Object , String )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
I looked up the KB
How To Use Automation to Get and to Set Office Document Properties with Visual C# .NET
http://support.microsoft.com/kb/303296/en-us
but it only explains how to retrieve a known existing DocumentProperty or create one,
doesn't say how to get list of all existing DocumentProperty names and values
Thanks,
-srinivas y.
sri