Read inbuilt document properties of word document in C#

Word document properties are the details which describe the identity of a word document. In this article we will discuss about how to read document properties of any word document in C#.  Every word document has some inbuilt properties and custom properties such as Title, subject, category, Author and comments etc. In Ms-Word application, you can see or edit the document properties by opening Advance Document properties window just like if you are working on word 2010 you will see this like:

Advance Document properties:

Custom properties:

In this article we will read and write the various document properties and will learn that how to make custom properties of any word document.
So lets start with taking a windows form and design like this:

And then we need to add the following references: Microsoft.office.Core and Microsoft.Office.Interop.Word from the ‘Add Reference..’ option from your solution explorer.

Microsoft.office.Core
Microsoft.Office.Interop.Word
System.Reflection

Code:

Make three private object

private Microsoft.Office.Interop.Word.Application Wapp;
private Microsoft.Office.Interop.Word.Document docWord;
object missing = System.Reflection.Missing.Value;

Write code on the click event of the browse button.
Create OpenFileDialog object and set the filter for word documents.

OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Word document |*.doc;*.docx";
dlg.ShowDialog();
 
Initialize word application class and word document objects
if (Wapp == null)
{
   Wapp = new Microsoft.Office.Interop.Word.ApplicationClass();
}
if (docWord == null)
{
   docWord = new Microsoft.Office.Interop.Word.Document();
}
else
{
   docWord.Close(ref missing, ref missing, ref missing);
}

Use Documents.open() method for opening word document and use BuiltInDocumentProperties property for retrieving its inbuilt advanced properties.

docWord = Wapp.Documents.Open(ref Filename, ref missing, ref missing,
		ref missing, ref missing, ref missing, ref missing, ref missing, 
		ref missing, ref missing, ref missing, ref missing, ref missing, 
		ref missing, ref missing, ref missing);
 
//Microsoft.Office.Core.DocumentProperties _BuiltInProperties;
 
object _BuiltInProperties = docWord.BuiltInDocumentProperties;
Type typeDocBuiltInProps = _BuiltInProperties.GetType();
 
try
{
  Object Authorprop  = typeDocBuiltInProps.InvokeMember("Item", 
		BindingFlags.Default| BindingFlags.GetProperty, null, _BuiltInProperties, 
        new   object[] { "Title" });
  Type typeAuthorprop = Authorprop.GetType();
 
  txtTitle.Text = typeAuthorprop.InvokeMember("Value", 
		BindingFlags.Default | BindingFlags.GetProperty, null, Authorprop, 
		new object[] { }).ToString();
 
  Object Subjectprop = typeDocBuiltInProps.InvokeMember("Item", 
		BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, 	
		new object[] { "Subject" });
  Type typeSubjectprop = Subjectprop.GetType();
 
  txtSubject.Text = typeSubjectprop.InvokeMember("Value", 	
		BindingFlags.Default | BindingFlags.GetProperty, null, Subjectprop, 
		new object[] { }).ToString();
 
  Object Managerprop = typeDocBuiltInProps.InvokeMember("Item", 
		BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, 
                new object[] { "Manager" });
  Type typeManagerprop = Managerprop.GetType();
  txtManager.Text = typeManagerprop.InvokeMember("Value", 	
		BindingFlags.Default | BindingFlags.GetProperty, null, Managerprop, 
		new object[] { }).ToString();
 
  Object CompanyProp = typeDocBuiltInProps.InvokeMember("Item", 
		BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties,
		new object[] { "Company" });
  Type typeCompanyProp = CompanyProp.GetType();
  txtCompany.Text = typeCompanyProp.InvokeMember("Value", 
		BindingFlags.Default | BindingFlags.GetProperty, null, CompanyProp, 
		new object[] { }).ToString();
 
  Object Keywordsprop = typeDocBuiltInProps.InvokeMember("Item",
		BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, 
		new object[] { "Keywords" });
  Type typeKeywordsprop = Keywordsprop.GetType();
  txtKeyWords.Text = typeKeywordsprop.InvokeMember("Value", 
		BindingFlags.Default | BindingFlags.GetProperty, null, Keywordsprop, 
		new object[] { }).ToString();
 
  Object Commentsprop = typeDocBuiltInProps.InvokeMember("Item", 
		BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, 
		new object[] { "Comments" });
  Type typeCommentsprop = Commentsprop.GetType();
  txtComment.Text = typeCommentsprop.InvokeMember("Value", 
		BindingFlags.Default | BindingFlags.GetProperty, null, Commentsprop,
		new object[] { }).ToString();
 
  Object CategoryProp = typeDocBuiltInProps.InvokeMember("Item",
		BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties,
		new object[] { "Category" });
  Type typeCategoryProp = CategoryProp.GetType();
  txtCategory.Text = typeCategoryProp.InvokeMember("Value",
		BindingFlags.Default | BindingFlags.GetProperty, null, CategoryProp,
		new object[] { }).ToString();
}
  • Ankur

    Good Article

    it help me a lot to star work on Word application with .net