实现代码如下:
unit WordOperate;
erface
uses SysUtils, ComObj, ShellApi, Windows;
//合并多个Word文件存到个指定文件里
//参数介绍说明
//ArrWord 为带路径Word文件名称
//outFileName 为合并后文件名称
//bInsertPageBreak 是否在合并文件的间插入分页符
//bOpenAfterMerge 是否合并的后打开文件
procedure MergeWord(ArrWord: .gif' /> of ; const outFileName: ; bInsertPageBreak: boolean = True; bOpenAfterMerge: Boolean = true);
Implementation
procedure MergeWord(ArrWord: .gif' /> of ; const outFileName: ; bInsertPageBreak: boolean = True; bOpenAfterMerge: Boolean = true);
Const
wdSectionBreakNextPage = $00000002;
Var
i: eger;
vFile: ;
WordApp: Variant;
bOpen: boolean;
begin
WordApp := CreateOleObject('Word.Application');
bOpen := False;
for i := Low(ArrWord) to High(ArrWord) do
begin
vFile := ArrWord[i];
FileExists(vFile) then
begin
not bOpen then
begin
//打开第个文件
WordApp.Documents.open(vFile);
bOpen := True;
end
begin
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;
bOpenAfterMerge then
begin
//打开合并后文件
ShellExecute(0,'Open',PChar(outFileName),nil,nil,SW_SHOWDEFAULT);
end;
end;
end.
最新评论