In this article we will discuss how to read a document's properties of any Word document in C#. Every Word document has some inbuilt properties and custom properties such as Title, subject, category, Author, comments etc.
You can see or set the document properties by opening Advance Document properties section just like if you are working with Word 2010 you will see this like:
Advance Document properties:
In this article we will read and set these properties and will learn how to make custom properties of any Word document.
So let's start with taking a windows form and design like above.
And then we need to add some references:
Microsoft.office.Core
Microsoft.Office.Interop.Word
System.Reflection
Code:
Make three private objects
private Microsoft.Office.Interop.Word.Application Wapp;
private Microsoft.Office.Interop.Word.Document docWord;
object missing = System.Reflection.Missing.Value;
Write code on the browse button click event
Create OpenFileDialog object and set the filter for Word documents.
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Word document |*.doc;*.docx";
dlg.ShowDialog();
Initialize thbe 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 the Documents.open() method for opening the Word document and use the BuiltInDocumentProperties property for retrieving it's 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();
}