Word的格式是安装微软的存储结构的方式存储的!以上的代码都是要安装Word,才能分析的!
下面的是真真不需要Word的!!!!
unit FindText;
interface
function FindTextInFile(const FileName, TextToFind: WideString): boolean;
implementation
uses ComObj, ActiveX, AxCtrls, SysUtils, Classes;
function FindTextInFile(const FileName, TextToFind: WideString): boolean;
var Root: IStorage;
EnumStat: IEnumStatStg;
Stat: TStatStg;
iStm: IStream;
Stream: TOleStream;
DocTextString: WideString;
begin
Result:=False;
if not FileExists(FileName) then Exit;
// Check to see if it's a structured storage file
if StgIsStorageFile(PWideChar(FileName)) <> S_OK then Exit;
// Open the file
OleCheck(StgOpenStorage(PWideChar(FileName), nil,
STGM_READ or STGM_SHARE_EXCLUSIVE, nil, 0, Root));
// Enumerate the storage and stream objects contained within this file
OleCheck(Root.EnumElements(0, nil, 0, EnumStat));
// Check all objects in the storage
while EnumStat.Next(1, Stat, nil) = S_OK do
// Is it a stream with Word data
if Stat.pwcsName = 'WordDocument' then
// Try to get the stream "WordDocument"
if Succeeded(Root.OpenStream(Stat.pwcsName, nil,
STGM_READ or STGM_SHARE_EXCLUSIVE, 0, iStm)) then
begin
Stream:=TOleStream.Create(iStm);
try
if Stream.Size > 0 then
begin
// Move text data to string variable
SetLength(DocTextString, Stream.Size);
Stream.Position:=0;
Stream.Read(pChar(DocTextString)^, Stream.Size);
// Find a necessary text
Result:=(Pos(TextToFind, DocTextString) > 0);
end;
finally
Stream.Free;
end;
Exit;
end;
end;
end.