C implements the method of merging multiple word documents
- 2020-10-31 21:57:40
- OfStack
This example shows how C# implements merging multiple word documents, which is a very useful technique. Share to everybody for everybody reference.
Specific implementation methods are as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using System.Diagnostics;
namespace driverexam.WordReport
{
public class WordDocumentMerger
{
private ApplicationClass objApp = null;
private Document objDocLast = null;
private Document objDocBeforeLast = null;
public WordDocumentMerger()
{
objApp = new ApplicationClass();
}
#region Open the file
private void Open(string tempDoc)
{
object objTempDoc = tempDoc;
object objMissing = System.Reflection.Missing.Value;
objDocLast = objApp.Documents.Open(
ref objTempDoc, //FileName
ref objMissing, //ConfirmVersions
ref objMissing, //ReadOnly
ref objMissing, //AddToRecentFiles
ref objMissing, //PasswordDocument
ref objMissing, //PasswordTemplate
ref objMissing, //Revert
ref objMissing, //WritePasswordDocument
ref objMissing, //WritePasswordTemplate
ref objMissing, //Format
ref objMissing, //Enconding
ref objMissing, //Visible
ref objMissing, //OpenAndRepair
ref objMissing, //DocumentDirection
ref objMissing, //NoEncodingDialog
ref objMissing //XMLTransform
);
objDocLast.Activate();
}
#endregion
#region Save the file to the output template
private void SaveAs(string outDoc)
{
object objMissing = System.Reflection.Missing.Value;
object objOutDoc = outDoc;
objDocLast.SaveAs(
ref objOutDoc, //FileName
ref objMissing, //FileFormat
ref objMissing, //LockComments
ref objMissing, //PassWord
ref objMissing, //AddToRecentFiles
ref objMissing, //WritePassword
ref objMissing, //ReadOnlyRecommended
ref objMissing, //EmbedTrueTypeFonts
ref objMissing, //SaveNativePictureFormat
ref objMissing, //SaveFormsData
ref objMissing, //SaveAsAOCELetter,
ref objMissing, //Encoding
ref objMissing, //InsertLineBreaks
ref objMissing, //AllowSubstitutions
ref objMissing, //LineEnding
ref objMissing //AddBiDiMarks
);
}
#endregion
#region Loop merge multiple files (copy merge duplicate files)
/// <summary>
/// Loop merge multiple files (copy merge duplicate files)
/// </summary>
/// <param name="tempDoc"> Template file </param>
/// <param name="arrCopies"> Files that need to be merged </param>
/// <param name="outDoc"> The merged output file </param>
public void CopyMerge(string tempDoc, string[] arrCopies, string outDoc)
{
object objMissing = Missing.Value;
object objFalse = false;
object objTarget = WdMergeTarget.wdMergeTargetSelected;
object objUseFormatFrom = WdUseFormattingFrom.wdFormattingFromSelected;
try
{
// Open the template file
Open(tempDoc);
foreach (string strCopy in arrCopies)
{
objDocLast.Merge(
strCopy, //FileName
ref objTarget, //MergeTarget
ref objMissing, //DetectFormatChanges
ref objUseFormatFrom, //UseFormattingFrom
ref objMissing //AddToRecentFiles
);
objDocBeforeLast = objDocLast;
objDocLast = objApp.ActiveDocument;
if (objDocBeforeLast != null)
{
objDocBeforeLast.Close(
ref objFalse, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RouteDocument
);
}
}
// Save to an output file
SaveAs(outDoc);
foreach (Document objDocument in objApp.Documents)
{
objDocument.Close(
ref objFalse, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RouteDocument
);
}
}
finally
{
objApp.Quit(
ref objMissing, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RoutDocument
);
objApp = null;
}
}
/// <summary>
/// Loop merge multiple files (copy merge duplicate files)
/// </summary>
/// <param name="tempDoc"> Template file </param>
/// <param name="arrCopies"> Files that need to be merged </param>
/// <param name="outDoc"> The merged output file </param>
public void CopyMerge(string tempDoc, string strCopyFolder, string outDoc)
{
string[] arrFiles = Directory.GetFiles(strCopyFolder);
CopyMerge(tempDoc, arrFiles, outDoc);
}
#endregion
#region Loop merge multiple files (insert merge file)
/// <summary>
/// Loop merge multiple files (insert merge file)
/// </summary>
/// <param name="tempDoc"> Template file </param>
/// <param name="arrCopies"> Files that need to be merged </param>
/// <param name="outDoc"> The merged output file </param>
public void InsertMerge(string tempDoc, string[] arrCopies, string outDoc)
{
object objMissing = Missing.Value;
object objFalse = false;
object confirmConversion = false;
object link = false;
object attachment = false;
try
{
// Open the template file
Open(tempDoc);
foreach (string strCopy in arrCopies)
{
objApp.Selection.InsertFile(
strCopy,
ref objMissing,
ref confirmConversion,
ref link,
ref attachment
);
}
// Save to an output file
SaveAs(outDoc);
foreach (Document objDocument in objApp.Documents)
{
objDocument.Close(
ref objFalse, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RouteDocument
);
}
}
finally
{
objApp.Quit(
ref objMissing, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RoutDocument
);
objApp = null;
}
}
/// <summary>
/// Loop merge multiple files (insert merge file)
/// </summary>
/// <param name="tempDoc"> Template file </param>
/// <param name="arrCopies"> Files that need to be merged </param>
/// <param name="outDoc"> The merged output file </param>
public void InsertMerge(string tempDoc, string strCopyFolder, string outDoc)
{
string[] arrFiles = Directory.GetFiles(strCopyFolder);
InsertMerge(tempDoc, arrFiles, outDoc);
}
#endregion
}
}
I believe that this article has a certain reference value for your C# programming.