X
logo
Master your Data
7 December 2009

Accessing Word Document Properties via Office11 Interop Library

To access Word Document Properties with the Office 2003 Interop Libraries for the .NET Framework you need to use reflection to access the collection and read the Name and Value properties of each item in the List.

For example to Return a List of Document Properties might do something like this.

Code Snippet
  1. public ICollection<DocumentProperty> GetDocumentProperties()
  2.  {
  3.      ICollection<DocumentProperty> returnValue = new List<DocumentProperty>();
  4.  
  5.      IEnumerable p = (IEnumerable)_Document.CustomDocumentProperties;
  6.  
  7.      foreach (object f in p)
  8.      {
  9.          Type item = f.GetType();
  10.          try
  11.          {
  12.              returnValue.Add(new DocumentProperty
  13.              {
  14.                  Name = GetProperty<string>(item, f, "Name"),
  15.                  Value = GetProperty<object>(item, f, "Value")
  16.              });
  17.          }
  18.          catch (TargetInvocationException)
  19.          {
  20.              //Do nothing
  21.          }
  22.          catch (COMException)         
  23.          {
  24.              //Do nothing
  25.          }
  26.      }
  27.  
  28.      return returnValue;
  29.  }
  30.  
  31.  private T GetProperty<T>(Type t, object o, string name)
  32.  {
  33.      return (T)t.InvokeMember(name,
  34.                         BindingFlags.Default |
  35.                         BindingFlags.GetProperty,
  36.                         null, o,
  37.                         null);
  38.  }
Related Blog Posts