Insert Word Document As Header Of another

Hi,
I would love some help on this. I’m trying to take the contents of one document and insert it as the header of another. I’m just doing a quick hack to see if it can be done:

public static void AddHeadderFooter(Document header, Document dest, HeaderFooterType type)
{
    dest.Sections[0].ClearHeadersFooters();
    HeaderFooter hf = new HeaderFooter(dest, type);
    Node n = dest.ImportNode(header.Sections[0], true, ImportFormatMode.KeepSourceFormatting);
    hf.AppendChild(n);
    dest.Sections[0].HeadersFooters.Add(hf);
}

but I am not allowed to append a Section Node into the HeaderFooter object.

What is the best way to do this? I would like to keep formatting and images etc from another document if possible?

Thanks,

Jules

Hi
Thanks for your request. I think that you should use the following code to achieve this.

public void AddHeadderFooter(Document header, Document dest, HeaderFooterType type)
{
    // Remove HeaderFoter nodes
    dest.FirstSection.HeadersFooters.Clear();
    // Create HeaderFooter
    HeaderFooter hf = new HeaderFooter(dest, type);
    // Import first section
    Section srcSection = (Section)dest.ImportNode(header.FirstSection, true, ImportFormatMode.KeepSourceFormatting);
    // Add content of section to HeaderFooter node
    while (srcSection.Body.HasChildNodes)
    {
        hf.AppendChild(srcSection.Body.FirstChild);
    }
    // Ad HeaderFooter to destination document
    dest.FirstSection.HeadersFooters.Add(hf);
}

I hope this could help you.
Best regards.

Hi Alexey,

Thanks for your quick response. I’ve tried what you have posted, things seem to run fine, but my resulting document has an empty header/footer. I can’t figure out why?

Thanks,

Jules

I’m sorry… there was a chair-to-keyboard error!

This works absolutely fine! Thank you very much.

Jules