应用程序可以把Word中的文本提取出来作为模糊查询的依据或其他操作。本例是单击“打开文档”按钮,从选中的Word文档中提取文本内容,如图7.27所示。
通过createoleobject方法创建OLE对象,通过OLE对象的Documents.Open方法打开所选中的Word文档,从文档中提取出文本内容显示在RichEdit1中,主要代码如下:
procedure TForm2.Button1Click(Sender: TObject);
var
wordapp, doc: olevariant;
strs: TStringList;
begin
if OpenDialog1.Execute then
begin
strs := TStringList.Create;
wordapp := createoleobject('Word.application');
try
doc := wordapp.Documents.Open(FileName := OpenDialog1.FileName);
RichEdit1.Text := doc.range.Text;
doc.Close;
finally
wordapp.quit;
strs.Free;
end;
end;
end;
图7.27 读取Word中的文本