Problem bringing styles from another document

Good morning,

I’m having some issues bringing the Headings from one document to another.

Let’s suppose I have 3 H1s and 1 H2. I’ll break the document into different sections based on the headings, THEN, I will create 3 new instances of the H1s and its content.

When I do that, it works like a charm, BUT, all the styles I’ve had in the original file won’t come up to the new Document.

Is there a way, when instantiating a Document, to bring ALL the styles from the original document?

Kind regards,
Davi Rodrigues

@swipezy,

Thanks for your inquiry. Please use the following code example to copy all styles from source document to destination document. Hope this helps you.

Document source = new Document(MyDir + "in.docx");

Document destination = new Document();
                 
foreach (Style style in source.Styles)
{
    if (style.Name.ToLower().Contains("char"))
        continue;
    var styleName = style.Name;
    var targetStyle = destination.Styles.AddCopy(style);
    targetStyle.Name = styleName;
}
destination.Save(MyDir + "18.4.docx");