You need to combine multiple Word documents into One Word document. Word documents can be merged in two forms:
1. Copy and merge;
One is insert merge, which merges multiple documents into another document in sequence.
The Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using Microsoft. Office. Interop. Word;
Using System. Reflection;
Using System. IO;
Using System. Diagnostics;
Namespace Eipsoft. Common
{
/// <Summary>
// Word document Merging
/// </Summary>
Public class WordDocumentMerger
{
Private ApplicationClass objApp = null;
Private Document objDocLast = null;
Private Document objDocBeforeLast = null;
Public WordDocumentMerger ()
{
ObjApp = new ApplicationClass ();
}
# Region open a 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 Merge multiple files cyclically (copy and merge duplicate files)
/// <Summary>
/// Merge multiple files cyclically (copy and merge duplicate files)
/// </Summary>
/// <Param name = "tempDoc"> template file &