Alternative ways to merge Word documents

Source: Internet
Author: User
Tags range

A problem today is that you need to append the contents of multiple Word documents to a target Word document, if I have the target document A.doc and many other documents B.doc,c.doc ... And so on a lot. This problem, if it is in the service side, the direct use of OPENXML technology, read and write documents can be achieved, so the performance is more stable, but need to have a certain understanding of OPENXML. If you are on the client machine, you can use the Word PIA implementation.

Because I am familiar with the word PIA, this method is used. But in the process of realization, there are also many kinds of ideas.

Open the B.doc, select the contents, copy to the Clipboard, then open the target file A.doc, move the cursor to the end of the document by code, and paste. Paste one at a time to save the file, and then open C.doc Repeat the above process to know that all files are added to complete.

Open the B.doc, select the contents, get the Range object, and open the target file A.doc, where you can insert the contents of B by code.

Both of these methods involve opening and reading documents to be merged. Once completed, the released resources need to be turned off in time. For complex objects, such as table objects with row or column merging, creating a table in the target document in the second way, and then looping through the tables in the document to be merged and assigning a value to the table in the target document can cause problems. The results are not very good.

After you've looked up some information, you'll find that word has the ability to add content from the document to the open document.

This is much simpler, and usually the easiest thing to do is to record a macro.

Action steps are as follows:

Click Record macro

Open target file A.doc

Then press and hold the keyboard ctrl+end key to jump to the end of document A.

Click Insert (Insert)->object (object)->text from file (text in files)

Select the file to insert B.doc, OK

Stop Recording macros

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

Click the macro Editor, and then you can see the code that corresponds to the step that we just operated. And then we're going to switch it. NET in the way it is called. This is a useful technique in office development.

Sub Macro1 ()
    selection.endkey unit:=wdstory
    changefileopendirectory "D:\"
    selection.insertfile Filename:= "B.docx", range:= "", _
        Confirmconversions:=false, Link:=false, Attachment:=false end
Sub

The key methods here are two, one is Selection.endkey, to jump to the end of the document, one is InsertFile, import text from the document. Convert the above VBA code into. NET C # code as follows:

private void Btncombine_click (object sender, EventArgs e) {//Create WordApp object word.application = null;
    try {WordApp = (Word.Application) System.Runtime.InteropServices.Marshal.GetActiveObject ("Word.Application");
    The catch (System.Runtime.InteropServices.COMException ex) {WordApp = new Word.Application ();
    }//source file, the file to be merged object missing = Type.Missing;
    Object TargetFileName = @ "D:\a.docx";
        Word.Document Doctarget = WordApp.Documents.Open (ref targetfilename, ref missing, ref missing, ref missing, ref missing,  Ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
    
    Missing, ref missing, ref missing);
        All the files to be merged into the source file, note the order//in the actual operation replace this part with the name of all the Word documents you want to merge under the Traversal folder for (int count = 0; count <= 3; count++) {
        String sourcefilename = @ "D:\b.docx"; Move the cursor to the end of the document DocTarget.Application.Selection.EndKey (wdunits.wdstory);
        Inserts the content to be merged at the end of the document DocTarget.Application.Selection.InsertFile (sourceFileName, ref missing, False, False, Fals
        e);
    Save Doctarget.save ();
    
    } doctarget.save ();
    Marshal.ReleaseComObject (WordApp);
MessageBox.Show ("Success"); }

What you need to be aware of in your code is how you create Word.Application objects, and how resources are freed.

Author: yangecnu (YANGECNU ' s blog on Blog Park)

Source: http://www.cnblogs.com/yangecnu/

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.