Microsoft Office Interop Word – Read Document Properties And Close

Details:

In the code below we will open a word document from your hard drive using C#. Word documents have built in document properties and custom properties. The block will read one property from each type as an example of how to read the properties.

Sample Code:



try
 {
     Microsoft.Office.Interop.Word.Document docs = wordOBJ.Documents.Open(file, nullobject, nullobject, nullobject,
     nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject);

     //get the Custom Property
     object customWordProperties = docs.CustomDocumentProperties;
     Type cusDocBuiltInProps = customWordProperties.GetType();
     Object dealcodeprop = cusDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, customWordProperties, new object[] { "dealcode" });
     Type typedealcodeprop = dealcodeprop.GetType();
     dealCode = typedealcodeprop.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, dealcodeprop, new object[] { }).ToString();

     //get the built in Property 
     object WordProperties = docs.BuiltInDocumentProperties;
     Type DocBuiltInProps = WordProperties.GetType();
     Object dealNameprop = DocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, WordProperties, new object[] { "Title" });
     Type typedealnameprop = dealNameprop.GetType();
     dealNameTitle = typedealnameprop.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, dealNameprop, new object[] { }).ToString();


     //Close the document and quit the process
     docs.Close(WdSaveOptions.wdDoNotSaveChanges, nullobject, nullobject);
     //  wordObject.Quit();
 }
 catch (Exception esd)
 {
     //docs.Close(WdSaveOptions.wdDoNotSaveChanges, nullobject, nullobject);
     //wordObject.Quit();
     var debug = esd.Message;
 }

 

Sample Code 2:

Pass in a word application instance.



object file = pathToFile; //this is the path
object nullobject = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open(file, nullobject, nullobject, nullobject,
nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject, nullobject);

//Get Author Name
object wordProperties = docs.BuiltInDocumentProperties;
Type typeDocBuiltInProps = wordProperties.GetType();

Object Authorprop = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, wordProperties, new object[] { "Author" });
// Type typeAuthorprop = Authorprop.GetType();
strAuthor = typeAuthorprop.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, Authorprop, new object[] { }).ToString();

//get the dealcode
Object dealcodeprop = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, wordProperties, new object[] { "dealcode" });
Type typedealcodeprop = dealcodeprop.GetType();
dealCode = typedealcodeprop.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, Authorprop, new object[] { }).ToString();

//Close the document and quit the process
docs.Close(WdSaveOptions.wdDoNotSaveChanges, nullobject, nullobject);
wordObject.Quit();

 

Resources: https://msdn.microsoft.com/VBA/Word-VBA/articles/application-quit-method-word