Setting Word Properties in C#?

Jan 17, 2010 at 6:05 AM

I write simple code to Open a word file(.docx) and set properties for that file. this code do exactly itself job. but when C# open Microsoft Word i should update all fields to see real values which are updated from my code!

I want to know, what i do in my code to when Microsoft Word is open, all properties have real values and no more need to update properties with mysself?

this is my code:

tip1:dictionary properties contain property name and value.

public void SetWordFile(string FilePath, Dictionary<string, object> properties)

        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 oDocBuiltInProps;
        object oDocAuthorProp;
        Type typeDocAuthorProp;

        oWord.Visible = true;

        object oFalse = false;
        object filePath = FilePath;

        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);

        oDocBuiltInProps = oDoc.CustomDocumentProperties;
        Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
        foreach (string item in properties.Keys)
            oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
                                    BindingFlags.Default |
                                    null, oDocBuiltInProps,
                                    new object[] { item });
            typeDocAuthorProp = oDocAuthorProp.GetType();

                                       BindingFlags.Default |
                                       null, oDocBuiltInProps,
                                       new object[] { item, properties[item] });
tip2: But when odoc open Microsoft word application, document show me previous values for properties 
(mean that values when i create each property for word file), while properties changed and have
real values and need to UPDATE all properties to see real values.