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/