合并多个 Word 文档,同时保留标题 - .Net 办公自动化

本文关键字:标题 保留 Net 办公自动化 Word 文档 合并 | 更新日期: 2023-09-27 18:36:59

我想合并多个word文档,同时保留每个文档的页眉和页脚。下面的代码正在合并文档,但也合并了页眉和页脚:

public static void Merge(List filesToMerge, string outputFilename)
{
    Application wordApplication = null;
    Document wordDocument = null;
    try
    {
        // Create a new Microsoft Word application object
        wordApplication = new Application();
        wordApplication.Visible = false;
        wordApplication.ScreenUpdating = false;
        // Create a new file based on our template
        object defaultTemplate = @"Normal.dotm";
        wordDocument = wordApplication.Documents.Add(ref defaultTemplate);
        // Make a Word selection object.
        Selection selection = wordApplication.Selection;
        // Loop thru each of the Word documents
        foreach (string file in filesToMerge)
        {
            // Insert the files to our template
            selection.InsertFile(file);
            object pageBreak = WdBreakType.wdSectionBreakNextPage;
            selection.InsertBreak(ref pageBreak);
        }
        // Save the document to it's output file.
        object outputFile = outputFilename;
        wordDocument.SaveAs(ref outputFile);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error while conversion. Details: " + ex);
    }
    finally
    {
        MSWordCleanup(wordApplication, wordDocument);
    }
}

有什么办法吗?

合并多个 Word 文档,同时保留标题 - .Net 办公自动化

完成以下培训课程可能会对您有所帮助:http://office.microsoft.com/en-us/word-help/headers-and-footers-from-basic-to-elaborate-RZ001021662.aspx

虽然这不会以"自动化形式"给你答案,但可能足以让你找出答案。