469,602 Members | 1,640 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

C# app: Reading and modifying MS Office document meta data

I'm trying to write a C# application that iterates through a collection of Microsoft Office documents and standardizes the meta data of those documents (e.g. the author and company name). Unfortunately, I can't seem to figure out what libraries/classes I should use to do this, if they exist at all.

I have looked at the Microsoft.Office.Core namespace, but it seems that namespace only gives you a bunch of interfaces and no actual classes.

Is there a standard way to change the meta data of Microsoft Office Documents? If so, could anyone explain it to me?

If not, and there are different methods for each type of Office document, then I think I can assume for my project that I'll only be dealing with Word, Excel, and PowerPoint documents. If I made that assumption, how would I edit the meta data of each of those three kinds of documents?

Also, is there a way to test whether a file is an Office document (or specifically, a Word, Excel, or PowerPoint document) without just looking at the file extension and trusting it to be correct?
Jan 10 '08 #1
4 21912
I've figured out a solution. It's funny, I only found one helpful source on the web and the solution they gave didn't quite work. Below I've written methods that set and get Word, Excel, and PowerPoint file properties.

Expand|Select|Wrap|Line Numbers
  1. object GetWordDocumentPropertyValue(Word.Document document, string propertyName)
  2. {
  3.   object builtInProperties = document.BuiltInDocumentProperties;
  4.   Type builtInPropertiesType = builtInProperties.GetType();
  5.   object property = builtInPropertiesType.InvokeMember("Item", BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
  6.   Type propertyType = property.GetType();
  7.   object propertyValue = propertyType.InvokeMember("Value", BindingFlags.GetProperty, null, property, new object[] { });
  8.   return propertyValue;
  9. }
  11. object GetExcelWorkbookPropertyValue(Excel.Workbook workbook, string propertyName)
  12. {
  13.   object builtInProperties = workbook.BuiltinDocumentProperties;
  14.   Type builtInPropertiesType = builtInProperties.GetType();
  15.   object property = builtInPropertiesType.InvokeMember("Item", BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
  16.   Type propertyType = property.GetType();
  17.   object propertyValue = propertyType.InvokeMember("Value", BindingFlags.GetProperty, null, property, new object[] { });
  18.   return propertyValue;
  19. }
  21. object GetPowerPointPresentationPropertyValue(PowerPoint.Presentation presentation, string propertyName)
  22. {
  23.   object builtInProperties = presentation.BuiltInDocumentProperties;
  24.   Type builtInPropertiesType = builtInProperties.GetType();
  25.   object property = builtInPropertiesType.InvokeMember("Item", BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
  26.   Type propertyType = property.GetType();
  27.   object propertyValue = propertyType.InvokeMember("Value", BindingFlags.GetProperty, null, property, new object[] { });
  28.   return propertyValue;
  29. }
  31. void SetWordDocumentPropertyValue(Word.Document document, string propertyName, string propertyValue)
  32. {
  33.   object builtInProperties = document.BuiltInDocumentProperties;
  34.   Type builtInPropertiesType = builtInProperties.GetType();
  35.   object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
  36.   Type propertyType = property.GetType();
  37.   propertyType.InvokeMember("Value", BindingFlags.SetProperty, null, property, new object[] { propertyValue });
  38.   document.UpdateSummaryProperties();
  39.   document.Save();
  40. }
  42. void SetExcelWorkbookPropertyValue(Excel.Workbook workbook, string propertyName, string propertyValue)
  43. {
  44.   object builtInProperties = workbook.BuiltinDocumentProperties;
  45.   Type builtInPropertiesType = builtInProperties.GetType();
  46.   object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
  47.   Type propertyType = property.GetType();
  48.   propertyType.InvokeMember("Value", BindingFlags.SetProperty, null, property, new object[] { propertyValue });
  49.   workbook.Save();
  50. }
  52. void SetPowerPointPresentationPropertyValue(PowerPoint.Presentation presentation, string propertyName, string propertyValue)
  53. {
  54.   object builtInProperties = presentation.BuiltInDocumentProperties;
  55.   Type builtInPropertiesType = builtInProperties.GetType();
  56.   object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
  57.   Type propertyType = property.GetType();
  58.   propertyType.InvokeMember("Value", BindingFlags.SetProperty, null, property, new object[] { propertyValue });
  59.   presentation.Save();
  60. }
This codes works with already-opened Word, Excel, and PowerPoint files, and opening them is another issue altogether, but a small amount of Googling will help you find the answer pretty quickly.
Jan 14 '08 #2
It should be noted that if you want to update the field that appears in Word 2007 as 'status' use the name 'Content Status' to access it.

Great post!
Jan 14 '09 #3
i want program in c# types of inheritance with get and set method and base consttructor should be called and use access specifier
Jul 18 '10 #4
u save my life bro. thank you so much..
Nov 3 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Shakil Khan | last post: by
2 posts views Thread by boarderstu | last post: by
4 posts views Thread by Dylan | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.