Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Find text in MS Word files, without Word app loadingGo to Dmitry Lifatov's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
OLE
Skill Level:
Scoring:
Last Update:
11/17/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet MS-Word MS-Excel MS-Office IStorage COM TOleStream
Times Scored:
5
Visits:
6848
Uploader: Dmitry Lifatov
Company: Business Master
Reference: N/A
 
Question/Problem/Abstract:
Easy way to find a text in MS Word
Answer:



You can work with MS-Office files without using Office app.

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.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
variable
    Will (Jan 14 2005 7:45PM)

Just a quick question, what is the variable S_OK? it doesnt seem to be declared anywhere?
Respond

RE: variable
Alex (Aug 8 2005 3:20PM)

S_OK is declared in windows.pas
as S_OK = $00000000;
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Kucherov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)