Merge multiple Word documents with C # Programming

Source: Internet
Author: User

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 &

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.