dotnet / dotnet/Open-XML-SDK

Macro-enabled document created with CreateFromTemplate() cannot be opened by Word

Open
#1,314 3 comments 0 reactions 1 assignee Claimed by @mikeebowen View on GitHub
Dominant language
C#
Stars
4.6k
Forks
605
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

As described in this [stackoverflow question](https://stackoverflow.com/questions/75158776/wordprocessingdocument-createfromtemplate-method-creates-corrupted-ms-word-files) and [my answer](https://stackoverflow.com/questions/75158776/wordprocessingdocument-createfromtemplate-method-creates-corrupted-ms-word-files/75160516#75160516), Microsoft Word does not open a macro-enabled document (.docm) created from a macro-enabled template (.dotm) with the `WordprocessingDocument.CreateFromTemplate()` method. When using the same code to create a "normal" document (.docx) from a template (.dotx), Word opens the document without any issue.

When validating both documents (i.e., .docm and .docx) with the `OpenXmlValidator`, no validation errors are reported (unless the template is attached, in which case one validation error related to the attached template is emitted in each case).

**To Reproduce**

```csharp
public sealed class CreateFromTemplateTests
{
private readonly ITestOutputHelper _output;

public CreateFromTemplateTests(ITestOutputHelper output)
{
_output = output;
}

[Theory]
[InlineData("c:\\temp\\MacroEnabledTemplate.dotm", "c:\\temp\\MacroEnabledDocument.docm")]
[InlineData("c:\\temp\\Template.dotx", "c:\\temp\\Document.docx")]
public void CanCreateDocmFromDotm(string templatePath, string documentPath)
{
// Let's not attach the template, which is done by default. If a template is attached, the validator complains as follows:
// The element has unexpected child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:attachedTemplate'.
using (var wordDocument = WordprocessingDocument.CreateFromTemplate(templatePath, false))
{
// Validate the document as created with CreateFromTemplate.
ValidateOpenXmlPackage(wordDocument);

// Save that document to disk so we can open it with Word, for example.
wordDocument.SaveAs(documentPath).Dispose();
}

using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentPath, true))
{
// Validate the document that was opened from disk, just to see what Word would open.
ValidateOpenXmlPackage(wordDocument);
}
}

private void ValidateOpenXmlPackage(OpenXmlPackage openXmlPackage)
{
OpenXmlValidator validator = new(FileFormatVersions.Office2019);
List validationErrors = validator.Validate(openXmlPackage).ToList();

foreach (ValidationErrorInfo validationError in validationErrors)
{
_output.WriteLine(validationError.Description);
}

if (validationErrors.Any())
{
// Note that Word will most often be able to open the document even if there are validation errors.
throw new Exception("The validator found validation errors.");
}
}
}
```

The above unit tests pass and no validation errors are reported.

I could not attach the templates. However, I only created super-simple templates with one line of text and a hello world macro in the case of the macro-enabled template.

**Observed behavior**

The Word document (.docx) created by the test can be opened as expected. The macro-enabled document (.docm) cannot be opened. In my case, Word neither displays the document nor reports any error. In the case of the original poster of the stackoverflow question, a runtime error seems to occur (which I could not reproduce).

**Expected behavior**

Using `WordprocessingDocument.CreateFromTemplate()`, the Open XML SDK should be able to create a macro-enabled document (.docm) from a valid, macro-enabled template (.dotm) that can be opened by Microsoft Word.

**Desktop (please complete the following information):**
- OS: Windows 10 (in my case)
- Office version: Word 365 version 16.0.14931.20648 32bit (OP on stackoverflow), Microsoft Word for M365 MSO (Version 2022 Build 16.0.14931.20858) 64-bit (in my case)
- .NET Target: .NET 7.0 and net472 (in my case)
- DocumentFormat.OpenXml Version: 2.19.0 (in both cases)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.