473,466 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

7 New Member
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 23144
amphibian1
7 New Member
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
mrsharps
1 New Member
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 New Member
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
farisyusry
1 New Member
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.