dotnet / dotnet/Open-XML-SDK

Open XML SDK to instantiate Word template with macros : either bad formatting or non-working macros

Open
#1,485 1 comment 0 reactions 1 assignee Assigned to @mkaszewiak View on GitHub
Dominant language
C#
Stars
4.6k
Forks
605
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
When instantiating a Word document based on a template which contains macros, either the formatting is not preserved or the macros are duplicated and not functionning.

**Screenshots**
Template with correct top part
![image](https://github.com/dotnet/Open-XML-SDK/assets/123658217/f2aa4219-4ceb-4815-89f0-0ddebf37ff83)

Instance created with bad top part
![image](https://github.com/dotnet/Open-XML-SDK/assets/123658217/030887c8-a7f2-4fe9-b80a-5fd4b5ed0e46)

Instance created with duplicated VBA macros
![image](https://github.com/dotnet/Open-XML-SDK/assets/123658217/9458eebb-f148-4dc9-9869-6e3c890b4fcb)

**To Reproduce**
```c#
var templatePath = @".\MyTemplate.dotm";
```
*First try : LAYOUT IS NOT PRESERVED FROM THE TEMPLATE AND MACRO WORKS FINE*
```c#
string tempFileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".Try1.docm");
File.Copy(templatePath, tempFileName, true);

using (var wordDocument = WordprocessingDocument.Open(tempFileName, true))
{
wordDocument.ChangeDocumentType(WordprocessingDocumentType.MacroEnabledDocument);
wordDocument.Save();
}
```
*Second try : LAYOUT IS NOT PRESERVED FROM THE TEMPLATE AND MACRO WORKS FINE*
```c#
tempFileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".Try2.docm");

using (var wordDocument = WordprocessingDocument.CreateFromTemplate(templatePath, false))
{
wordDocument.ChangeDocumentType(WordprocessingDocumentType.MacroEnabledDocument);
wordDocument.SaveAs(tempFileName).Dispose();
}
```
*Third try : LAYOUT IS PRESERVED FROM THE TEMPLATE BUT MACROS ARE DUPLICATED*
```c#
tempFileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".Try3.docm");

using (var wordDocument = WordprocessingDocument.CreateFromTemplate(templatePath))
{
wordDocument.ChangeDocumentType(WordprocessingDocumentType.MacroEnabledDocument);
wordDocument.SaveAs(tempFileName).Dispose();
}
```

**Observed behavior**
Either the formatting of the instance is not correct or the VBA macros are not working because they seem to be duplicated.

**Expected behavior**
An instance of the document with a correct formatting and working macros.

**Desktop (please complete the following information):**
- OS: Windows 10 22H2
- Office version : Microsoft® Word for Microsoft 365 MSO (Version 2208 Build 16.0.15601.20676) 64-bit
- .NET Target: 4.7.2
- DocumentFormat.OpenXml Version: v4.0.30319

**Additional information**
I zipped my template into the attached document.
[MyTemplate.zip](https://github.com/dotnet/Open-XML-SDK/files/11934115/MyTemplate.zip)

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.