Use C # To merge Word documents

Source: Internet
Author: User

Program code

Introduction:
In my current project, the customer wants the Administrator to publish files on the website, and the customer's articles (no matter what changes or additions) can upload the modified versions back to the website. Then, the administrator can browse the abstract about the article modification after school and allow the Administrator to approve or deny the modification.
Although I initially thought it would be very difficult to implement it, I found that in addition to the "tracking changes" feature of the word, it is possible to combine several documents into one, all changes in such a document are allowed. Of course, this method can be applied through VBA;. Net can also be implemented.
Solution:


To do this, I used Visual Studio. NET 2003 to create a C # ASP. NET project:
In a shell, default. the apsx page is the home page, and docmerger implements the true merge operation. It is in the "Files" folder ("originaldoc" contains the original version of the file, "copies" stores user-uploaded files, and "output" is a document with a summary added ).
The website allows everyone to download the original document, upload a modified version, and create an output document.


The server selects the "track changes" option to release the introduced version, so that each change of the user will be easily identified.
The following is the implementation code:


Private void btngo_click (Object sender, system. eventargs E)
{
String strbasedir = server. mappath ("files/Copies ");
String strfilename = guid. newguid (). tostring (). Replace ("{", ""). Replace ("}","");
Upload. postedfile. saveas (path. Combine (strbasedir, strfilename + ". Doc "));
}
In the previous example, the three users modified the document respectively.
User 1

User 2

User 3

The "copies" folder contains copies uploaded by different users.


The docmerger class has a merge method that is used for text party merging. I write a folder overload operation instead of a file list.
Collapse


/// <Summary>
/// Merge a document with a copy file
/// </Summary>
/// <Param name = "strorgdoc">
/// Original file name
/// </Param>
/// <Param name = "arrcopies">
/// Modified file name
/// </Param>
/// <Param name = "stroutdoc">
/// Result file name
/// </Param>
Public void Merge (string strorgdoc, string [] arrcopies, string stroutdoc)
{
Applicationclass objapp = NULL;

// Boxing of default values for com InterOP purposes
// Set the initial value for the COM Component
Object objmissing = missing. value;
Object objfalse = false;
Object objtarget = wdmergetarget. wdmergetargetselected;
Object objuseformatfrom = wduseformattingfrom. wdformattingfromselected;

Try
{
Objapp = new applicationclass ();
Object objorgdoc = strorgdoc;

Document objdoclast = NULL;
Document objdocbeforelast = NULL;

Objdoclast = objapp. Documents. Open (
Ref objorgdoc, // file name
Ref objmissing, // confirm the version
Ref objmissing, // read-only
Ref objmissing, // Add to the nearest File
Ref objmissing, // Password File
Ref objmissing, // password detection Board
Ref objmissing, // reply
Ref objmissing, // write the password file
Ref objmissing, // enter the password
Ref objmissing, // format
Ref objmissing, // Encrypt
Ref objmissing, // visible
Ref objmissing, // open and fix
Ref objmissing, // Party A directly
Ref objmissing, // unencrypted dialog box
Ref objmissing // XML Transfer
);

Foreach (string strcopy in arrcopies)
{
Debug. writeline ("merging file" + strcopy );
Objdoclast. Merge (
Strcopy, // file name
Ref objtarget, // merge targets
Ref objmissing, // detect format changes
Ref objuseformatfrom, // format table
Ref objmissing // Add it to the latest file
);
Objdocbeforelast = objdoclast;
Objdoclast = objapp. activedocument;
Debug. writeline ("the active document is" + objdoclast. Name );

If (objdocbeforelast! = NULL)
{
Debug. writeline ("Closing" + objdocbeforelast. Name );
Objdocbeforelast. Close (
Ref objfalse, // Save the changes
Ref objmissing, // original format
Ref objmissing // path document
);
}


}

Object objoutdoc = stroutdoc;

Objdoclast. saveas (
Ref objoutdoc, // file name
Ref objmissing, // File Format
Ref objmissing, // lock the Directory
Ref objmissing, // Password
Ref objmissing, // Add to current file
Ref objmissing, // write the password
Ref objmissing, // read-only recommendation
Ref objmissing, // embed the True Type
Ref objmissing, // Save the local image format
Ref objmissing, // save table data
Ref objmissing, // s Save As aoce
Ref objmissing, // Encrypt
Ref objmissing, // insert row ends
Ref objmissing, // can be replaced
Ref objmissing, // the end of the row
Ref objmissing // Add a bidi tag
);

Foreach (document objdocument in objapp. Documents)
{
Objdocument. Close (
Ref objfalse, // Save the changes
Ref objmissing, // original format
Ref objmissing // path document
);
}

}
Finally
{
Objapp. Quit (
Ref objmissing, // Save the changes
Ref objmissing, // original format
Ref objmissing // path document
);
Objapp = NULL;
}
}

/// <Summary>
/// Merge a document with a copy file
/// </Summary>
/// <Param name = "strorgdoc">
/// Original file name
/// </Param>
/// <Param name = "arrcopies">
/// Modified file name
/// </Param>
/// <Param name = "stroutdoc">
/// Result file name
/// </Param>
Public void Merge (string strorgdoc, string strcopyfolder, string stroutdoc)
{
String [] arrfiles = directory. getfiles (strcopyfolder );
Merge (strorgdoc, arrfiles, stroutdoc );
}

The webform invokes this method:

Private void btnmerge_click (Object sender, system. eventargs E)
{
String strorigfile = server. mappath ("files/originaldoc/thedocument.doc ");
String strcopiesdir = server. mappath ("files/Copies ");
String stroutputdir = server. mappath ("files/output/output.doc ");
Docmerger objmerger = new docmerger ();
Objmerger. Merge (strorigfile, strcopiesdir, stroutputdir );
Lnkresult. navigateurl = "files/output/output.doc ";
Lnkresult. Visible = true;
}

The final output is a document party that has been modified and merged by all users:


In this way, the administrator can check all changes, approve or reject them at one time.
The code here contains the content described in all articles, but you still need to keep it in your mind:
I have extended the functions of Office 2003. You must re-import the Office Type Library (I think it should be running in Office XP, 2000 or even 97) and re-compile the project
Microsoft has some shortcomings in office network automation. However, all tests I perform are necessary. However, if necessary, the docmerger class can be transplanted to other projects to turn the home page into a program interface.
Conclusion:
With the automation of. NET Framework, it is easy to complete further office operations, and it allows fast transmission and excellent operation.

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.