帮忙到底,
说明:定义标签的对对象必需是(一个任意字符或空格或回车符),不充许是单独的光标位置。
这样作窗体上放一个 WordApplication1 改名为WordApp
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleServer, Word2000, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
WordApp: TWordApplication;
OpenDialog1: TOpenDialog;
WordDocument1: TWordDocument;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
folderName: String;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
templateName: OleVariant;
newTemplate: OleVariant;
vSelection: Selection;
ItemIndex: OleVariant;
vBookMark: BookMark;
I : Integer;
tt,len,ts: OleVariant;
begin
folderName := ExtractFilePath(Application.ExeName); //当前路径下找模板
if not FileExists(folderName+'目标.doc') then
begin
MessageDlg('错误:找不到模板资源文件 目标.doc !', mtError, [mbOK], 0);
Close;
end;
templateName := folderName + '目标.doc';
newTemplate := False;
try
wordApp.Connect();
except
beep;
MessageDlg('您的计算机上还未安装Microsoft Office Word97或更高的版本!', mtError, [mbOK], 0);
Abort;
end;
wordApp.Visible:=true;
wordApp.WindowState := 1;
wordApp.Documents.AddOld(templateName,newTemplate);
wordApp.Caption := '走过大海!';
vSelection := wordApp.Selection;
//================================================================ 插入要写入的内容
for I := 1 to wordapp.ActiveDocument.Bookmarks.Count do
begin
ItemIndex := I;
//取指定Index的书签
vBookMark := wordapp.ActiveDocument.Bookmarks.Item(ItemIndex);
if LowerCase(vBookMark.Name) = 'b1' then
begin
vBookMark.Select();
vSelection.InsertBefore('水温');
end;
if LowerCase(vBookMark.Name) = 'b2' then
begin
vBookMark.Select();
vSelection.InsertBefore('气温');
end;
if LowerCase(vBookMark.Name) = 'b4' then
begin
vBookMark.Select();
vSelection.InsertBefore('abcedesksksks');
end;
end;
//==================================================================查找替换
tt:=wdCharacter;
ts:=wdExtend;
for I := 1 to wordapp.ActiveDocument.Bookmarks.Count do
begin
ItemIndex := I;
//取指定Index的书签
vBookMark := wordapp.ActiveDocument.Bookmarks.Item(ItemIndex);
if LowerCase(vBookMark.Name) = 'b1' then
begin
vBookMark.Select();
len:=length(vbookmark.Range.Text)-1;
vSelection.MoveLeft(tt,len,ts);
vSelection.Delete(tt,len);
vSelection.InsertBefore('水温温');
end;
if LowerCase(vBookMark.Name) = 'b2' then
begin
vBookMark.Select();
len:=length(vbookmark.Range.Text)-1;
vSelection.MoveLeft(tt,len,ts);
vSelection.Delete(tt,len);
vSelection.InsertBefore('气温温');
end;
if LowerCase(vBookMark.Name) = 'b4' then
begin
vBookMark.Select();
len:=length(vbookmark.Range.Text)-1;
vSelection.MoveLeft(tt,len,ts);
vSelection.Delete(tt,len);
vSelection.InsertBefore('气温温');
end;
end;
//==========================================================================
wordApp.Disconnect();
end;
end.
//好用的话记得给分吆 [
]