Failing to Add a Custom Document Property

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
 
C

Cindy M -WordMVP-

Hi Codex,
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:
Maybe you should SAVE the document before quitting the application?
(doc.SaveAs)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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