By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

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

P: 7
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
Share this Question
Share on Google+
4 Replies


P: 7
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. }
  10.  
  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. }
  20.  
  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. }
  30.  
  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. }
  41.  
  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. }
  51.  
  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

P: 1
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

P: 1
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

P: 1
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.