0
votes

I need to create an empty template for using it later in my application. I am using the following code to create the template:-

    using (WordprocessingDocument template = WordprocessingDocument.Create(templatePath,
DocumentFormat.OpenXml.WordprocessingDocumentType.MacroEnabledTemplate, true))
    { 
        template.Close(); 
    }

But, later in the application, when I try to create a document out of this template, I receive the error mentioned next:-

Globals.ThisAddIn.Application.Documents.Add(templatePath));

The error is:

Word was unable to read this document. It may be corrupt.

1
This is resolved, we need to add MainDocumentPart, Document and Body to the template, then it will load correctly.teenup

1 Answers

0
votes

We need to add MainDocumentPart, Document and Body to the template as these are the minimum requirements for a valid document/template:-

var mainPart = schemaTemplate.AddMainDocumentPart();
var document = mainPart.Document = new wp.Document();
document.AppendChild<wp.Body>(new wp.Body());
schemaTemplate.Close();