How to: Validate a word processing document (Open XML SDK)

Last modified: July 27, 2012

Applies to: Office 2013 | Open XML

This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically validate a word processing document.

The following assembly directives are required to compile the code in this topic.

No code example is currently available or this language may not be supported.

This code example consists of two methods. The first method, ValidateWordDocument, is used to validate a regular Word file. It doesn’t throw any exceptions and closes the file after running the validation check. The second method, ValidateCorruptedWordDocument, starts by inserting some text into the body, which causes a schema error. It then validates the Word file, in which case the method throws an exception on trying to open the corrupted file. The validation is done by using the Validate method. The code displays information about any errors that are found, in addition to the count of errors.

In your main method, you can call the two methods, ValidateWordDocument and ValidateCorruptedWordDocument by using the following example that validates a file named "Word18.docx.".

No code example is currently available or this language may not be supported.
Important noteImportant

Notice that you cannot run the code twice after corrupting the file in the first run. You have to start with a new Word file.

Following is the complete sample code in both C# and Visual Basic.

No code example is currently available or this language may not be supported.

Contribute to this article

Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub.

Show: