Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Getting MS Word Document Properties Using Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
3.96/5 (11 votes)
25 Jan 20062 min read 124.6K   2.6K   37   18
This code will demonstrate how to automate and get the Document properties from a MS Word document.

Introduction

This code demonstrates how to extract/retrieve the Document properties from a MS Word document file. Refer to my other articles for a proper background on MS Office Automation and how to setup your development environment if needed. I have created this as a utility which is being used for a much larger project in our company for document management. The night I was asked to create this utility, I also received a lot of request during that same week from readers on CodeProject to show them this exact feature! So I hope this helps demonstrate yet another way to extract/automate Word for your needs.

Background

No special background is necessary. Just have some hands on experience with C#.

Using the code

The following is a listing of the code to retrieve the document properties. Refer to Automating MS Word Using Visual Studio .NET to get started with a new project. I have provided source code that will demonstrate in a very simple fashion how to achieve this task. The following listing is the main section of the program which actually does exactly what we are looking for.

Note: The following code was written for MS Word 2003.

C#
...

/// <summary>
/// Get source document. Open a FileDialog window
/// for user to select single/multiple files for
/// parsing.
/// </summary>
private void butSourceDocument_Click(object sender, System.EventArgs e)
{
    openFileDialog.Multiselect = true;

    if( openFileDialog.ShowDialog() == DialogResult.OK )
    {
        
        object    vk_read_only    = false;
        object    vk_visible        = true;
        object    vk_false            = false;
        object    vk_true            = true;
        object    vk_dynamic        = 2;

        object    vk_missing        = System.Reflection.Missing.Value;
        
        string [] properties = { "Title", "Subject", "Author", 
                  "Keywords", "Revision Number", 
                  "Creation Date", "Last Save Time" };

        using (StreamWriter sw = new StreamWriter("FileProperties.txt")) 
        {
            string strHeader = null;
            foreach( string header in properties )
            {
                strHeader = strHeader + header + ", ";
            }
            
            sw.WriteLine(strHeader.Substring( 0, strHeader.Length-2 ));

            foreach( string file in openFileDialog.FileNames )
            {
                object fileName = @file;

                // Let make the word application visible
                vk_word_app.Visible = false;
                
                // Let's open the document
                Word.Document vk_my_doc = 
                    vk_word_app.Documents.Open( ref fileName,
                    ref vk_missing, ref vk_read_only, 
                    ref vk_missing, ref vk_missing,
                    ref vk_missing, ref vk_missing, 
                    ref vk_missing, ref vk_missing,
                    ref vk_missing, ref vk_missing, 
                    ref vk_visible );

                object vk_document_prop = vk_my_doc.BuiltInDocumentProperties;

                Type propertyType = vk_document_prop.GetType(  );
        
                string strProValues = null;
                foreach( string prop in properties )
                {
                    object property = propertyType.InvokeMember( "Item", 
                        System.Reflection.BindingFlags.Default | 
                        System.Reflection.BindingFlags.GetProperty, 
                        null, 
                        vk_document_prop, 
                        new object[  ] { prop } );

                    Type validatedType = property.GetType(  );

                    string propValue = validatedType.InvokeMember( "Value", 
                        System.Reflection.BindingFlags.Default | 
                        System.Reflection.BindingFlags.GetProperty, 
                        null, 
                        property, 
                        new object[] {} ).ToString(  );

                    strProValues = strProValues + propValue + ", ";
                }
                sw.WriteLine(strProValues.Substring(0,strProValues.Length-2));
        
                // close the original document
                vk_my_doc.Close( ref vk_false, ref vk_missing, ref vk_missing );
            }
        }

        // close word application
        vk_word_app.Quit( ref vk_false, ref vk_missing, ref vk_missing );

        MessageBox.Show( "Done!" );
    }
}

...

A quick summary, the program has a set of properties that it is looking for and wants to extract. In this case, they are defined as string [] properties = { "Title", "Subject", "Author", "Keywords", "Revision Number", "Creation Date", "Last Save Time" };. The program then loops through the selected file(s) and extracts the information and stores it in a text file for further processing. If you notice, there are two for loops in the code shown above, the first one is for the list of files to process, and the second one is for the list of the properties to extract on each file.

Points of Interest

The new version of Office, Office 2003, is going to make things a little easier for Office developers. So if you are an Office developer, you should start looking into the features that Office 2003 has to offer. One of the nice features that I like is the capability of exporting documents into XML format.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Noorcon Inc.
United States United States
Published Books:

Introduction to Game Programing: Using C# and Unity 3D designed and developed to help individuals that are interested in the field of computer science and game programming. It is intended to illustrate the concepts and fundamentals of computer programming. It uses the design and development of simple games to illustrate and apply the concepts.

Book Preview:
Preview all chapters

Available from:
Amazon.com
Barnes and Noble Book Store
Amazon Kindle (eBook)
iTunes - iBook (eBook)

Vahé Karamian
www.noorcon.com
www.facebook.com/NoorconInc

Comments and Discussions

 
QuestionRead Document Properties without opening the document? Pin
hmacintosh16-May-11 7:51
hmacintosh16-May-11 7:51 
QuestionImportant Pin
Jesusel419-Oct-06 11:37
Jesusel419-Oct-06 11:37 
GeneralSetting MS Word Document Properties Pin
ComLog10-May-06 4:48
ComLog10-May-06 4:48 
QuestionRe: Setting MS Word Document Properties Pin
sirobert25-Jun-07 2:14
sirobert25-Jun-07 2:14 
AnswerRe: Setting MS Word Document Properties Pin
sirobert25-Jun-07 3:08
sirobert25-Jun-07 3:08 
JokeRe: Setting MS Word Document Properties Pin
Siegfried Glaser1-Oct-08 1:10
Siegfried Glaser1-Oct-08 1:10 
GeneralRe: Setting MS Word Document Properties Pin
Johnny Glenn15-Mar-12 23:46
Johnny Glenn15-Mar-12 23:46 
QuestionForm fields processing Pin
infal5-Feb-06 13:19
infal5-Feb-06 13:19 
GeneralRe: Form fields processing Pin
Ryan at Unilever9-Feb-06 0:58
Ryan at Unilever9-Feb-06 0:58 
GeneralRe: Form fields processing Pin
infal9-Feb-06 5:51
infal9-Feb-06 5:51 
GeneralBulk processing office document properties Pin
Ryan at Unilever2-Feb-06 22:20
Ryan at Unilever2-Feb-06 22:20 
JokeRe: Bulk processing office document properties Pin
Vahe Karamian6-Feb-06 7:43
Vahe Karamian6-Feb-06 7:43 
GeneralRe: Bulk processing office document properties Pin
digitaldragon11-Feb-06 18:29
digitaldragon11-Feb-06 18:29 
AnswerRe: Bulk processing office document properties Pin
Ryan at Unilever13-Feb-06 23:35
Ryan at Unilever13-Feb-06 23:35 
AnswerRe: Bulk processing office document properties Pin
Ryan at Unilever13-Feb-06 23:52
Ryan at Unilever13-Feb-06 23:52 
GeneralChanging Pin
Michael J. Collins31-Jan-06 4:05
professionalMichael J. Collins31-Jan-06 4:05 
GeneralRe: Changing Pin
Vahe Karamian31-Jan-06 7:10
Vahe Karamian31-Jan-06 7:10 
GeneralAbout the Article Pin
Vahe Karamian25-Jan-06 13:16
Vahe Karamian25-Jan-06 13:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.