我用的是bookmark,例程如下:
procedure TForm1.butSaveClick(Sender: TObject);
var
ItemIndex:OleVariant;
i,j: integer;
TempDoc,NewTempDoc,TempWord,TempEmpty:OleVariant;
TempW:OleVariant;
Temp1,Temp2,Temp3,Temp4:OleVariant;
TempTemp : string;
Tt : String;
T: OleVariant;
begin
try
for j := 0 to RzListView1.Items.Count-1 do
begin
TempEmpty := EmptyParam;
TempDoc := EmptyParam;
NewTempDoc := True;
ItemIndex := 1;
try
Wordapplication1.Connect;
except
MessageDlg('无法打开Word,请确认已安装Word。', mtError, [mbOk], 0);
Abort;
end;
//打开指定WORD文档
TempWord := Edit1.Text ;
//WordApplication1.Visible := True;
WordApplication1.Documents.Open(TempWord,TempEmpty,TempEmpty,
TempEmpty,TempEmpty,TempEmpty,TempEmpty,
TempEmpty,TempEmpty,TempEmpty);
TempDoc := EmptyParam;
NewTempDoc := False;
TempWord := wdGoToBookmark;
//方法:用Bookmarks;
//i := wordapplication1.ActiveDocument.Bookmarks.Count;
//showmessage(inttostr(i));
for i:= 0 to wordapplication1.ActiveDocument.Bookmarks.Count-1 do
begin
T := i+1;
TempEmpty := EmptyParam;
//取出bookmard的名字
TempW := WordApplication1.ActiveDocument.Bookmarks.Item(T).Name;
IF TempW = 'c' then
begin
TempTemp := RzListView1.Items[j].Caption;
wordapplication1.Selection.GoTo_(TempWord,TempEmpty,TempEmpty,TempW);
wordapplication1.Selection.TypeText(TempTemp);//'一个好人'
end;
if TempW = 'd' then
begin
TempTemp := RzListView1.Items[j].SubItems.Strings[0];
wordapplication1.Selection.GoTo_(TempWord,TempEmpty,TempEmpty,TempW);
wordapplication1.Selection.TypeText (TempTemp);//'一个好人';
end;
end;
//保存退出WORD
try
wordapplication1.ChangeFileOpenDirectory('C:/My Documents');
Temp1 := 'b.rtf';
Temp2 := wdFormatDocument;
Temp3 := '';
Temp4 := True;
TempW := False;
WordApplication1.ActiveDocument.SaveAs(Temp1,Temp2,TempW,
Temp3,Temp4,Temp3,TempW,TempW,TempW,TempW,TempW);
wordApplication1.Quit;
WordApplication1.Disconnect;
showmessage('保存成功');
except
on E: Exception do
begin
Showmessage(E.Message);
WordApplication1.Disconnect;
end;
end;
end;
except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication1.Disconnect;
end;
end;
end;