Access the properties of a word with C #

1

Good afternoon,

I need to read the properties and values of them in a word document, so far I have only managed to access the name of them but not their value. Thanks in advance for the help.

This is the code I have:

Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document oDoc;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
object oMissing = Missing.Value;
object saveChanges = false;
object oDocAuthorProp;
Type typeDocAuthorProp;

        oWord.Visible = false;

        object oFalse = false;
        object filePath = @"C:\Users\Ales\Documents\PRUEBAS 40257.doc";

        try
        {

            oDoc = oWord.Documents.Open(ref filePath, ref oMissing, ref oFalse, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            oDoc.Activate();

            var oDocBuiltInProps = oDoc.BuiltInDocumentProperties;
            Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
            foreach (var item in oDocBuiltInProps)
            {
                string nombre = item.Name;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
asked by Miguel Ales 09.03.2017 в 17:22
source

1 answer

1

create a class of this type:

class type_archives
{
    string tipo, valor;

    public string Tipo
    {
        get
        {
            return tipo;
        }

        set
        {
            tipo = value;
        }
    }

    public string Valor
    {
        get
        {
            return valor;
        }

        set
        {
            valor = value;
        }
    }
}

Then, in the method where you look for the properties add this:

List<Clases.type_archives> archivos = new List<Clases.type_archives>();
foreach (var item in oDocBuiltInProps)
{
    try
    {
         Clases.type_archives archivo = new Clases.type_archives();
         archivo.Tipo = item.Name;
         archivo.Valor = item.Value;
         archivos.Add(archivo);
    }
    catch { }
}
    
answered by 09.03.2017 / 17:43
source