C
Codex Twin
Hello
I am trying to add a Custom Document Property to a Word document using C#.
The code I have written seems to run without errors. But after completion,
the document property just hasn't been added. This is the code I've written:
private void AddWordProperty(string filePath, string propertyName, string
propertyValue)
{
Word.Application word;
Word._Document doc;
object missing = System.Reflection.Missing.Value;
object objTrue = true;
object customProps;
object objFilePath = filePath;
//Create an instance of the Word object and make it visible
word = new Word.Application();
word.Visible = true;
//open document
doc = word.Documents.Open(ref objFilePath, 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);
customProps = doc.CustomDocumentProperties;
Type typeCustomProperties = customProps.GetType();
//Add a Custom property/value pair to the document
object[] args = {propertyName, false,
Office.MsoDocProperties.msoPropertyTypeString, propertyValue};
typeCustomProperties.InvokeMember("Add",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, customProps, args);
//Quit the application
word.Quit(ref objTrue, ref missing, ref missing);
}
I have tweaked a number of things to get it to work, but no joy.
What am I doing wrong?
Thanks
I am trying to add a Custom Document Property to a Word document using C#.
The code I have written seems to run without errors. But after completion,
the document property just hasn't been added. This is the code I've written:
private void AddWordProperty(string filePath, string propertyName, string
propertyValue)
{
Word.Application word;
Word._Document doc;
object missing = System.Reflection.Missing.Value;
object objTrue = true;
object customProps;
object objFilePath = filePath;
//Create an instance of the Word object and make it visible
word = new Word.Application();
word.Visible = true;
//open document
doc = word.Documents.Open(ref objFilePath, 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);
customProps = doc.CustomDocumentProperties;
Type typeCustomProperties = customProps.GetType();
//Add a Custom property/value pair to the document
object[] args = {propertyName, false,
Office.MsoDocProperties.msoPropertyTypeString, propertyValue};
typeCustomProperties.InvokeMember("Add",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, customProps, args);
//Quit the application
word.Quit(ref objTrue, ref missing, ref missing);
}
I have tweaked a number of things to get it to work, but no joy.
What am I doing wrong?
Thanks