uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ComObj, ComConst,ShellAPI,Word2000 ;
procedure TForm1.Button1Click(Sender: TObject);
var
WordApp: Variant;//word对象
Jpath:string;//word文件路径
Jword:string;//要查询的字段
myrange:variant;
flag:integer;//文档中是否有所查字段的判断标志
i:integer;//字段所在的当前页号
a,b,c,aaa:integer;
begin
try
Jpath:=edit1.text;//获得WORD文件路径
Jword:=edit2.Text;//获得所查字段
WordApp:=CreateOleObject('Word.Application');
WordApp.Documents.Open(Jpath);
memo1.Lines.clear;
a := WordApp.Selection.Information[wdActiveEndPageNumber] ;
b := WordApp.Selection.Information[wdFirstCharacterLineNumber];
c := WordApp.Selection.Information[wdFirstCharacterColumnNumber] ;
Memo1.Lines.Add(IntToStr(a));
Memo1.Lines.Add(IntToStr(b));
Memo1.Lines.Add(IntToStr(c));
aaa := WordApp.Selection.Information[wdNumberOfPagesInDocument] ;
Memo1.Lines.Add(IntToStr(aaa));
myRange:=WordApp.ActiveDocument.Content;
flag:=0;
while myRange.Find.Execute('sub') <> False do//查询 //(FindText:=Jword,Forward:=True
begin
i:=myRange.Information[wdActiveEndPageNumber];//获得字段所在页号
b := myRange.Information[wdFirstCharacterLineNumber];
c := myRange.Information[wdFirstCharacterColumnNumber] ;
memo1.Lines.add(Jword+'在第'+inttostr(i)+'页,'+inttostr(b)+'行,'+inttostr(c)+'列。');
flag:=flag+1;
end;
if flag=0 then
application.messagebox('没有找到。','查询结果',mb_ok);
finally
wordApp.Quit;
wordApp:=unassigned;
end;
end;