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.