unit WordOperate;
//作者:邦畿千里
//2008-10
interface
uses SysUtils, ComObj, ShellApi, Windows;
//合并多个Word文件,存到一个指定文件里
//参数说明
//ArrWord 为带路径的Word文件名称数组
//outFileName 为合并后的文件名称
//bInsertPageBreak 是否在合并的文件之间插入分页符
//bOpenAfterMerge 是否合并之后打开文件
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
//打开第一个文件
WordApp.Documents.open(vFile);
bOpen := True;
end
else
begin
if bInsertPageBreak then
begin
//插入分页符
WordApp.ActiveDocument.Paragraphs.Last.Range.InsertBreak(
wdSectionBreakNextPage);
end;
//插入一个文件
WordApp.ActiveDocument.Paragraphs.Last.Range.InsertFile(
vFile, '', False, false, false);
end;
end;
end;
//存为目标文件
WordApp.ActiveDocument.SaveAs(outFileName);
WordApp.Quit;
if bOpenAfterMerge then
begin
//打开合并后的文件
ShellExecute(0,'Open',PChar(outFileName),nil,nil,SW_SHOWDEFAULT);
end;
end;
end.