views:

217

answers:

1

I wrote simple code to open a Word file (.docx) and set properties for that file. This code do exactly itself job. But when C# opens 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:

tip: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 |
                                    BindingFlags.GetProperty,
                                    null, oDocBuiltInProps,
                                    new object[] { item });
            typeDocAuthorProp = oDocAuthorProp.GetType();

            typeDocAuthorProp.InvokeMember("Item",
                                       BindingFlags.Default |
                                       BindingFlags.SetProperty,
                                       null, oDocBuiltInProps,
                                       new object[] { item, properties[item] });
            Thread.Sleep(10);
        }
}
+1  A: 

After a long time...

I found my real big problem.(mean my co-worker found this solution)

After foreach, we should use this code to update all properties

for(int i=1;i<=Counter;i++)
  oDoc.Fields[i].Update();

with special thank from my co-worker

Rev
Thanks to the co-worker ;) I was looking for that same answer.
Nordes