breaststroke breaststroke - 1 month ago 17
C# Question

How to repair a word file?

I am using Microsoft Office 2010 PIA to convert word and excel documents to PDF. I faced corrupted files. In Excel we can repair, then open corrupted files:

Missing missing = Missing.Value;
Application excel = new Application();
Workbook workbook = excel.Workbooks.Open(sourceFilePath,
missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing,
missing, missing, missing, XlCorruptLoad.xlRepairFile);


Is it possible to repair corrupted doc and docx files ? I didn't find anything in Word Interop

Answer

The standard Open method has an OpenAndRepair parameter which you can set to True to repair the document, if necessary, when opening it.

Word also has the method Documents.OpenNoRepairDialog which you could use instead of the standard Documents.Open in order to suppress the user prompt.

Note: Whether either of these is actually able to repair the document will depend on what kind of damage is present. Also, you need to be aware that some repairs will actually change the content (remove damaged things, for example), so that the document can be opened.

Comments