Windows Phone Developers

Tuesday, December 16, 2008

Get Built-in Property of Word Document using C# (.NET)

Retrieve Built-in Property of Word Document using C# (.NET)

Here is a simple code that uses System.Reflection class’s methods to extract the builtin document properties of a Word document

Declarations

using System.Reflection;

using Microsoft.VisualBasic;

using Office = Microsoft.Office.Core ;

using Word = Microsoft.Office.Interop.Word;

Code:

private static void GetBuiltInProperty_CSharp(Object sFileName )

{

Word.Application oWA;// Word Application Object

Word.Document oWD;// Word Document Object

Object missing = Missing.Value ;

Object bSaveChanges = false ;

Object oBuiltProps;

try

{

oWA = new Word.Application();

oWA.Visible = true;

oWD = oWA.Documents.Open(ref sFileName , 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);

oBuiltProps = oWD.BuiltInDocumentProperties;

string sProperty = "Author";

Type typeBuiltProps = oBuiltProps.GetType();

Object oDocProp = typeBuiltProps.InvokeMember("Item", BindingFlags.DefaultBindingFlags.GetProperty, null, oBuiltProps, new object[] {sProperty});

string sPropValue;

Type typeAutValue = oDocProp.GetType();

sPropValue = typeAutValue.InvokeMember("Value", BindingFlags.Default BindingFlags.GetProperty, null, oDocProp, new object[] { }).ToString();

oWD.Close(ref bSaveChanges, ref missing, ref missing);

oWA.Quit(ref bSaveChanges, ref missing, ref missing);

}

catch (Exception)

{

throw;

}

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

2 comments:

  1. i'm getting two errors in using u r code

    ReplyDelete
  2. Narayana please post the errors you get. Would be great if you can post lines where the error occurs

    ReplyDelete