C# 合并多个Word文档,同时保留标题-.Net Office Automation
我想合并多个word文档,同时保留每个文档的页眉和页脚。下面的代码正在合并文档,但也合并了页眉和页脚:C# 合并多个Word文档,同时保留标题-.Net Office Automation,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我想合并多个word文档,同时保留每个文档的页眉和页脚。下面的代码正在合并文档,但也合并了页眉和页脚: public static void Merge(List filesToMerge, string outputFilename) { Application wordApplication = null; Document wordDocument = null; try { // Create a new Microsoft Word a
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);
}
}
还有其他方法吗?完成这些培训课程可能会对您有所帮助:
虽然这不会以“自动化形式”为您提供答案,但您只需找出答案即可。感谢您分享培训课程。分节符应该足以确保不同的页眉和页脚,但在我的情况下,我有一个文档没有任何页眉或页脚,但在合并时,它从上一页获取页眉和页脚。你知道如何保持这个页面没有页眉和页脚吗?@sh_kamalh恐怕我从来没有在Word中对页眉/页脚做过任何“精心设计”,这就是为什么我倾向于寻求办公室帮助的原因。我只能猜测。您是否尝试过在没有虚拟标题的情况下添加虚拟标题并将其删除?