Merge multiple Word Files

Source: Internet
Author: User

Unit wordoperate;

// Author: Qianli

// 2008-10

Interface

Uses sysutils, comobj, shellapi, windows;

//Merge multiple Word files and save them to a specified file.
// Parameter description
// Arrword is an array of Word file names with paths
// Outfilename is the name of the merged file.
// Whether binsertpagebreak inserts a paging character between merged files
// Whether bopenaftermerge is merged to open the file
Procedure mergeword (arrword: array of string; const outfilename: string; binsertpagebreak: Boolean = true; bopenaftermerge: Boolean = true );

 

Implementation

 

Procedure mergeword (arrword: array of string; const outfilename: string; binsertpagebreak: Boolean = true; bopenaftermerge: Boolean = true );
Const
Wdsectionbreaknextpage =00000002;
VaR
I: integer;
Vfile: string;
Wordapp: variant;
Bopen: Boolean;
Begin

Wordapp: = createoleobject ('word. application ');
Bopen: = false;
For I: = low (arrword) to high (arrword) Do
Begin
Vfile: = arrword [I];
If fileexists (vfile) then
Begin
If not bopen then
Begin

// Open the first file
Wordapp. Documents. Open (vfile );
Bopen: = true;
End
Else
Begin
If binsertpagebreak then

Begin

// Insert a paging character
Wordapp. activedocument. Paragraphs. Last. range. insertbreak (

Wdsectionbreaknextpage );

End;

 

// Insert an object
Wordapp. activedocument. Paragraphs. Last. range. insertfile (

Vfile, '', false );
End;
End;
End;

// Save as the target file
Wordapp. activedocument. saveas (outfilename );
Wordapp. Quit;
If bopenaftermerge then
Begin

// Open the merged File
ShellExecute (0, 'open', pchar (outfilename), nil, nil, sw_showdefault );
End;
End;
End.

 

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.