Hi,
i'm using C# and Word 2007. I have a document (58 Pages) and a custom table of content (table with 3 columns, last column always has PageRef to Bookmark).
At the dot line (see below) i tried
- activeDoc.Fields.Update();
- activeDoc.Fields.Update in separate Task (parallel)
- foreach field in activeDoc.Fields and field.update
- foreach field in activeDoc.Fields and field.update in separate task
- lots of Thread.Sleep
totally confused by the results
- sometimes no field is updated
- sometimes the first field is updated and all the other fields have the value of the first field
- sometimes the first two fields are updated and all other fields have the value of the last updated field
- sometimes half of the fields are updated, the other fields have the value of the last updated field (How many fields are updated depends on the milliseconds in the Thread.Sleep...)
This behavior makes totally no sense to me. It seems that there is something done asynchronous. If i debug my steps, all fields are updated correctly.
Does anybody has similar experiences?
Does anybody can explain, why just Fields.Update and SaveAs is not working and why i need a lot of threads and Thread.Sleeps???
I just want update all fields. Here is my base code
var application = new Microsoft.Office.Interop.Word.Application();
application.Visible = false;
try
{
var activeDoc = application.Documents.Open(fileName);
.....................................
activeDoc.SaveAs(Path.Combine(filePath, fileNameWoExt + "a.docx"));
}
finally
{
application.Quit();
}
UPDATE:
I just realized something very interesting. i checked the word option to recalculate fields on print preview. i opened my document in word, all fields are not updated (Every Page was 0). i opened print preview and the first field was updated and all the
other fields had the same value as the first.
So maybe it is no development problem...
i just use PAGEREF-Fields...