2

I would like some help to retrieve the properties of a word document. I have gotten as far as getting the title, subject and author. But I can't seem to get the "Date Last Saved" property and I don't know how to get a list of the property names.

My code looks like this:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop;
using System.Reflection;
using System.IO;


namespace MetaDataSorter
{
    class Program
    {

    static void Main(string[] args)
    {
        String dirName = @"H:\projekt\test raw files";

        String fileNameString = @"H:\projekt\raw files\vgahm\1 NTFS\Raw Files\Microsoft Word Document\1-300\FILE006.DOC";
        object fileName = (object)fileNameString;

        object missing = System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();


        Microsoft.Office.Interop.Word.Document aDoc = null;

        if (File.Exists((string)fileName))
        {
            DateTime toDay = DateTime.Now;

            object readOnly = false;
            object isVisible = false;

            wordApp.Visible = false;

            aDoc = wordApp.Documents.Open(ref fileName, ref missing, 
                ref readOnly, 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);

            aDoc.Activate();

            //object property = getWordDocumentPropertyValue(aDoc, "Title");

            System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Title"));
            System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Subject"));
            System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Author"));
            //System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Date Last Saved"));

            aDoc.Close();
        }

    }

    private static String getWordDocumentPropertyValue(Microsoft.Office.Interop.Word.Document document, string propertyName)
    {
        object builtInProperties = document.BuiltInDocumentProperties;

        Type builtInPropertiesType = builtInProperties.GetType();

        object property = builtInPropertiesType.InvokeMember("Item", BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });

        Type propertyType = property.GetType();
        object propertyValue = propertyType.InvokeMember("Value", BindingFlags.GetProperty, null, property, new object[] { });
        return propertyValue.ToString();
    }

}
}

How should I go about to get a list of the property values?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user1028037
  • 119
  • 3
  • 13

2 Answers2

1

You could probably start by having a look at class BuiltInDocumentProperties or use this link as a starting point, http://support.microsoft.com/default.aspx?scid=kb;en-us;303294

Remember that some properties are specific to certain products within the office suite and some are common. The one you are looking for is certainly a common one.

About word, find the list here http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.wdbuiltinproperty%28v=office.11%29.aspx

aelena
  • 86
  • 1
  • 3
  • 1
    Thanks! That didn't work though. I had problem with adding references which are needed to use the office stuff. I solved it however by stumbling upon http://www.codeproject.com/KB/office/MsWordAutoDocProp.aspx?fid=260729&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick&fr=18 which showed me I could use "Last Save Time", that was the property name :) – user1028037 Nov 08 '11 at 09:47
-1

Paste this code in a VBA module into a Word document and you get the list of properties.

Sub ListeProprietes()
   Dim proDoc As DocumentProperty

   For Each proDoc In ActiveDocument.BuiltInDocumentProperties

        MsgBox (proDoc.Name)

   Next

End Sub
doug65536
  • 6,562
  • 3
  • 43
  • 53