How can I check if some fields in my word have errors? I have a large document that contains many references to other chapters or images. When those chapters or images are missing in the document, the fields containing those references will display Error! Reference Source Not Found instead of the reference.
The problem is, that I need to create an algorithm that will check for those reference errors, no matter what the locale and language of the file is. The problem is, that this field error is localized in the language of the system of the user who uses the word.
How can I do this? Is there any property on Field that can be used to check if the source is available?
Currently, I check for errors in the fields by using the result text of the field:
Int32 fieldErrors = 0;
foreach (Word.Field field in doc.Fields)
{
field.Update();
if (field.Result.Text.StartsWith("Error!"))
++fieldErrors;
}
Unfortunately, this will only work in english word instances.
In the documentation for
Fieldtypes it is seen that aFieldinstance has anUpdate()method that returns abool. The documentation does not state what the semantic meaning of the return value is, however, by doing a short empirical study I found that the method returnstrueif theUpdate()succeeded andfalseif the update did not succeed. This means that in order to find fields with errors you can do something like:… or shorter with LINQ:
Another (and faster) approach would be to use the
Update()method exposed by theFieldscollection.… the method returns the index of the first field with an error. If no errors are found, the method returns 0.
For complete documentation please see the MSDN references: