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

Free Support Forum - aspose.com

CopyStylesFromTemplate - fails unless you access the style beforehand

This is similar to WORDSNET-19193, which was deemed user error.

See the attached project for a demonstration of the failure.

If you use CopyStylesFromTemplate, some styles are wrong unless you access those styles before the call. I believe this only applies to styles which are default.

In our example DemonstrateSuccess(),
Heading 3 only has the correct bold setting after CopyStylesFromTemplate
but only if you call
_ = mainDoc.Styles["Heading 3"]; beforehand…

        static void Main(string[] args)
        {
            DemonstrateSuccess();
            DemonstrateFailure();
        }

        public static void DemonstrateSuccess()
        {
            Document mainDoc = new Document("MyNormal.dotm");
            Document newDoc = new Document();
            _ = mainDoc.Styles["Heading 3"];
            newDoc.CopyStylesFromTemplate(mainDoc);
            Assert.AreEqual(mainDoc.Styles["Heading 3"].Font.Bold, newDoc.Styles["Heading 3"].Font.Bold);
        }
        public static void DemonstrateFailure()
        {
            Document mainDoc = new Document("MyNormal.dotm");
            Document newDoc = new Document();

            newDoc.CopyStylesFromTemplate(mainDoc);
            Assert.AreEqual(mainDoc.Styles["Heading 3"].Font.Bold, newDoc.Styles["Heading 3"].Font.Bold);
        }

CopyStylesFromTemplateBug.zip (5.9 MB)

@GMiddleton You are right this is exactly the same issue as WORDSNET-19193. You document MyNormal.dotm actually does not contain Heading 3 style. You can check this using code like the following:

Document doc = new Document(@"C:\Temp\MyNormal.dotm");
foreach (Style s in doc.Styles)
    Console.WriteLine(s.Name);

Also, you can unzip your document and explore styles.xml.
When you touch Heading 3 it is automatically created by Aspose.Words.

Also, your template is created by MS Word 2016, the empty document create by Aspose.Words is created by MS Word 2003, these versions of MS Word used different styles. You can make your test pass if you add OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016). See the following code:

Document mainDoc = new Document("MyNormal.dotm");
Document newDoc = new Document();
newDoc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016);

newDoc.CopyStylesFromTemplate(mainDoc);
Assert.AreEqual(mainDoc.Styles["Heading 3"].Font.Bold, newDoc.Styles["Heading 3"].Font.Bold);