How to loop through builtin- and custom properties in word 2003 via c#

K

Ken

I am developing a class library which should be able to extract all the
documentproperties which has been defined for any given office document.

The Extract method below uses the DocumentPropertiesToXml

public XmlDocument Extract(object fileName)
{
object readOnly = true;
object isVisible = true;

// Here is the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;

// Make word visible, so you can see what's happening
app.Visible = false;

// Open the document that was chosen by the dialog
Word.Document doc = app.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 isVisible, ref missing, ref
missing, ref missing, ref missing);


// Get the xml for documentproperties
return
XmlConverter.DocumentPropertiesToXml((object)doc.BuiltInDocumentProperties,
(object)doc.CustomDocumentProperties);
}

public static XmlDocument DocumentPropertiesToXml (object BuiltInProperties,
object CustomProperties)
{
StringBuilder xmlProperties = new StringBuilder();
xmlProperties.Append ("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
xmlProperties.Append ("<DocumentProperties>");
xmlProperties.Append ("<BuiltInProperties>");

Type typeDocBuiltInProps = BuiltInProperties.GetType();

int propCount = (Int32)typeDocBuiltInProps.InvokeMember("Count",
BindingFlags.Default | BindingFlags.GetProperty,
null,
BuiltInProperties,
new object[] {});

// Here I get the count of builtin properties which is all fine
// but I much rather prefer to do a
//
// foreach (DocumentProperties p in BuiltInDocumentProperties)
// {
// xmlProperties.Append("<" + p.Name + ">" + p.Value.ToString() +
"</" + p.Name + ">");
// }

xmlProperties.Append ("</BuiltInProperties>");
xmlProperties.Append ("<CustomProperties>");

/*foreach (DocumentProperty prop in CustomProperties)
{
xmlProperties.Append("<" + prop.Name + ">" + prop.Value.ToString() +
"</" + prop.Name + ">");
}*/

xmlProperties.Append ("</CustomProperties>");
xmlProperties.Append ("</DocumentProperties>");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlProperties.ToString());
return xmlDoc;
}

How do I call Word(2003) via reflection to get the
wApp.ActiveDocument.BuiltInDocumentProperties collection
back to C#. I can get single values but as soon as I try with a complex
datatype I am unable to cast it to something I can
use. It comes as object (COM_object).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top