tfordyce
tfordyce

Reputation: 1

Error when inserting multiple documents into one main DocX generated word document

I'm creating a program that builds a main document for a list of sub-documents/pages that are also generated by the program. The individual documents generate fine, but when I merge them - final document is corrupted.

This is a .net-core project, using nuget package DocX by xceed.

private void documentsMerge(string outputPath, List<string> pathList)
{
           DocX docToCreate = DocX.Create(outputPath);
           foreach (var path in pathList)
           {
                 if (pathList.IndexOf(path) != 0)
                 {
                   docToCreate.InsertParagraph().InsertPageBreakAfterSelf();
                 }
                 DocX docToMerge = DocX.Load(path);
                 foreach (var p in docToMerge.Paragraphs)
                 {
                    p.StyleName = "Normal";
                 }
                 docToCreate.InsertDocument(docToMerge);
           }
           foreach (var delFile in pathList)
           {
              File.Delete(delFile);
           }
           docToCreate.SaveAs(outputPath);
}

The expected result is a .docx word document that can be opened without error.

The error messages I get are:

MS office can't open file because some parts are missing/invalid part: /word/customizations.xml, Line 2, Column: 196 On recovery: "Show repairs - errors detected in file, Word could open after repairs "Styles 1"

Upvotes: 0

Views: 902

Answers (0)

Related Questions