473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 23128
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
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
akilan
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
u save my life bro. thank you so much..
Nov 3 '14 #5

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

Similar topics

5
by: Pugi! | last post by:
Hi, I would like to upload office-document (doc, xls, ...) using a form to a website (apache, php, mysql) in a specific directory and if possible insert it into a table (MySQL-db). Is this...
0
by: Shakil Khan | last post by:
Hi there ... My question is about Meta Data which is automatically saved with some files. For example,when an MS Office Documents is saved, it automaticaly save some extra information with the...
0
by: Stedak | last post by:
My application seems to freeze when Microsoft Office Document Imaging is set as the default for loading images. If the Windows Picture and Fax Viewer is used there is no problem. When I set a break...
2
by: VM | last post by:
Is it possible to view an Office document in a web form? I'm looking for a control that will allow me to show a Word doc or PDF dynamically.in my web form. It's like an document embedded into the...
9
by: tubby | last post by:
Silly question, but here goes... what's a good way to determine when a file is an Open Office document? I could look at the file extension, but it seems there would be a better way. VI shows this...
2
by: boarderstu | last post by:
Hi All, I'm looking for some tuts or advice on reading Meta data from files - preferably Exif data, any body got any advice on this? ta
4
by: Dylan | last post by:
Hello, I was trying to do a WCF tutorial (http://wcf.netfx3.com/content/ BuildingHelloWorld.aspx). I need to get the meta data from my service usin svcutil.exe why is not working? Please see...
0
samurai jack
by: samurai jack | last post by:
Hi all champs I am trying to print one doc using Microsoft office document Image writer. But it opens the document ( *.prn file ) after spooling and before sending it to print. And does not give...
0
by: palamadai | last post by:
hii ppl, I would like to create a password protected open office document and write some data to the document.After writing I would like to read the same document in C#.net 3.5 windows app.I would...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.