mercredi 23 avril 2014

Ne peut pas jour un champ à la fois avec VSTO pour Word - Stack Overflow


When fields are nested, there is a problem.


foreach (Word.Field field in this.Application.ActiveDocument.Fields)
{
field.Update();
text = field.Result.Text;

}

The above code does not work.


The process starts, but winds up in an endless loop or some other process that hangs the system.


Thinking about it, I can surmise that when you update a field, it might have an effect on the fields collection - thus, the loop fails.


Does anyone have any ideas on implementing this?


P.S. I know there is a Document.UpdateFields() method to update ALL fields. However, there are reasons why I cannot use this and need to only update specific field types.


My apologies! I was going to give an example of a nested field but was trying to test some more before sending anyone (Jack) on a goose-chase.


I waited and waited and waited, and after a good 2 or 3 minutes, it finished. After the last field, it crashed with this message:


Object has been deleted.


The error was generated from the following line inside the loop:


string text = field.Code.Text;


The template is being tested on mergefields that are not being found because I am testing without database connectivity. It would be odd, but explainable, that it goes through all the fields and then, at the end of the day, the very OUTER IF field's result is "Error! Reference source not found." But I still don't get why this could happen.


Nor do I understand why looping takes 3 minutes while a call to document.Fields.Update() will do the same thing in about 1 second and NOT result in the error described above.


Again, my apologies. I never considered updating inside a loop would be vastly slower that a call to doc.fields.update().



When fields are nested, there is a problem.


foreach (Word.Field field in this.Application.ActiveDocument.Fields)
{
field.Update();
text = field.Result.Text;

}

The above code does not work.


The process starts, but winds up in an endless loop or some other process that hangs the system.


Thinking about it, I can surmise that when you update a field, it might have an effect on the fields collection - thus, the loop fails.


Does anyone have any ideas on implementing this?


P.S. I know there is a Document.UpdateFields() method to update ALL fields. However, there are reasons why I cannot use this and need to only update specific field types.


My apologies! I was going to give an example of a nested field but was trying to test some more before sending anyone (Jack) on a goose-chase.


I waited and waited and waited, and after a good 2 or 3 minutes, it finished. After the last field, it crashed with this message:


Object has been deleted.


The error was generated from the following line inside the loop:


string text = field.Code.Text;


The template is being tested on mergefields that are not being found because I am testing without database connectivity. It would be odd, but explainable, that it goes through all the fields and then, at the end of the day, the very OUTER IF field's result is "Error! Reference source not found." But I still don't get why this could happen.


Nor do I understand why looping takes 3 minutes while a call to document.Fields.Update() will do the same thing in about 1 second and NOT result in the error described above.


Again, my apologies. I never considered updating inside a loop would be vastly slower that a call to doc.fields.update().


0 commentaires:

Enregistrer un commentaire