We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Word 2019 freezes after opening document generated with MailMerge.Execute (.NET)

Hi

I have a service which fills docx template with provided data.
Template is just a file with title page, table of contents page, empty page data inserted into and final page.
Everything was all right until I switched to new template.
After that words started freezing after opening of generated document - no logs, no errors, just freeze and process killed from task manager.

Problem appears when following conditions are met:

  1. document.MailMerge.Execute method is called
  2. document is long enough ( ~10 pages is enough for problem to appear )
  3. Word 2019 is used (there were no problems during test on Word 2016)

I managed to reproduce problem on latest version of Aspose.Words (21.9.0) and prepared code (below) which causes problem.
See my template here: Template_PL.docx (45.6 KB)

using Aspose.Words;
using Aspose.Words.Layout;
using System.IO;

namespace BrokenGeneration
{
    class Program
    {
        static void Main(string[] args)
        {
            var asposeWordsLicense = new Aspose.Words.License();
            asposeWordsLicense.SetLicense("..\\..\\..\\Aspose.Words.Product.Family.lic");

            var generator = new ProblemGenerator();
            generator.Generate("..\\..\\..\\Template_PL.docx", "..\\..\\..\\Generated.docx");
        }

        class ProblemGenerator
        {
            private DocumentBuilder _documentBuilder;

            public void Generate(string templatePath, string targetPath)
            {
                var fileBytes = File.ReadAllBytes(templatePath);
                var stream = new MemoryStream(fileBytes);
                var document = new Document(stream);
                _documentBuilder = new DocumentBuilder(document);

                SetLocationInDocument(document);

                GenerateContent();

                //if line below is commented out - generated file opens without any trouble
                document.MailMerge.Execute(new []{ "ProcessName" } , new [] { "dummy process name" });

                ApplyChanges(document);

                SaveDocument(document, targetPath);
            }

            private void GenerateContent()
            {
                for (var i = 0; i < 10; i++)
                {
                    RepeatedContent(i + 1);
                }
            }

            private void RepeatedContent(int i)
            {
                _documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
                _documentBuilder.Writeln($"Header{i}");
                _documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
                _documentBuilder.Writeln($"Text{i}");
                _documentBuilder.Writeln($"Text{i}");

                _documentBuilder.Font.ClearFormatting();
                _documentBuilder.ParagraphFormat.ClearFormatting();
                _documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
                _documentBuilder.Font.Bold = true;
                _documentBuilder.Writeln("Header Level 2");
                _documentBuilder.Font.ClearFormatting();
                _documentBuilder.ParagraphFormat.ClearFormatting();

                _documentBuilder.Font.Bold = true;
                _documentBuilder.Writeln($"Text{i}");
                _documentBuilder.Font.Bold = false;

                _documentBuilder.Writeln($"Text{i}");
            }

            private void SetLocationInDocument(Document document)
            {
                var generatedContentPageStart = GetNodeByPageNumber(3, document);
                _documentBuilder.MoveTo(generatedContentPageStart);
            }

            private static Node GetNodeByPageNumber(int page, Document document)
            {
                LayoutCollector lc = new LayoutCollector(document);

                foreach (Section section in document.Sections)
                {
                    foreach (Node node in section.Body.ChildNodes)
                    {
                        if (lc.GetStartPageIndex(node) == page)
                            return node;
                    }
                }

                return null;
            }

            private void ApplyChanges(Document document)
            {
                document.UpdateFields();
                document.UpdatePageLayout();
            }

            private void SaveDocument(Document document, string targetPath)
            {
                using (var stream = new MemoryStream())
                {
                    document.Save(stream, SaveFormat.Docx);

                    stream.Seek(0, SeekOrigin.Begin);

                    File.WriteAllBytes(targetPath, stream.ToArray());
                }
            }
        }
    }
}

@abruks

We have tested the scenario using the latest version of Aspose.Words for .NET 21.9 and have not found the shared issue. Please make sure that you are using the same template document and code. We have attached the output DOCX with this post for your kind reference.
21.9.docx (33.2 KB)

Thanks for response!

Attached file makes my Word freeze.
Word version: Word 2019, Build 16.0.10377.20023 64 bit.
Checked on 3 different machines with same results.
Could you share your word version?

@abruks

The output document can open at our end. Please check the attached image.
MS word.png (37.2 KB)

Could you please share the screenshot of the issue?

Sure, see screenshots attached.
On my environment it is enough to open document and scroll back and forth for less than 5 seconds.
On some documents time before freeze was a little bit longer - up to 1 minute.
Freeze1.PNG (32.6 KB)
Freeze2.PNG (28.1 KB)

@abruks

We have check the output document on different systems using MS Word 2019 and have not found the shared issue. Please check the attached image. ms-word 2019.png (31.9 KB)

Please install the latest updates of MS Word 2019. Hope this helps you.