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








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 (0)


replace text/font in doc-fileGo to Mike Shkolnik'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:
07/15/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet MS Word find replace doc
Times Scored:
5
Visits:
5488
Uploader: Mike Shkolnik
Company: Scalabium Software
Reference: http://www.scalabium.com/faq/dct0152.htm
 
Question/Problem/Abstract:
How can I find and replace some text in doc-file?
Answer:



Today I want to show how use an OLE automation for MS Word and replace some
text string in any document.

1. you must create OLE object for MS Word application:
WordApp := CreateOLEObject('Word.Application');


2. you must load a document from your file
WordApp.Documents.Open(yourDocFileName);


3. you must specify all parameters for find/replace - text string, range etc
WordApp.Selection.Find.ClearFormatting;

WordApp.Selection.Find.Text := yourStringForSearch;
WordApp.Selection.Find.Replacement.Text := yourNewStringForReplace;

WordApp.Selection.Find.Forward := True;
WordApp.Selection.Find.MatchAllWordForms := False;
WordApp.Selection.Find.MatchCase := Flase;
WordApp.Selection.Find.MatchWildcards := False;
WordApp.Selection.Find.MatchSoundsLike := False;
WordApp.Selection.Find.MatchWholeWord := False;
WordApp.Selection.Find.MatchFuzzy := False;
WordApp.Selection.Find.Wrap := wdFindContinue;
WordApp.Selection.Find.Format := False;


If you want to change a font instead text, use Font property of
WordApp.Selection.Find.Replacement instead Text
In such manner you may set a red background for all 'Adam Smith' strings,
for example

4. start a find/replace process
WordApp.Selection.Find.Execute(Replace := wdReplaceAll)

or replace a first occur only:
WordApp.Selection.Find.Execute(Replace := wdReplaceOne);


5. if you want to check if text was found then use Found method:
if WordApp.Selection.Find.Found then

  
<do something>


6. save a modified document
WordApp.ActiveDocument.SaveAs(yourDocFileName);


7. close MS Word instance
WordApp.ActiveDocument.Close;

WordApp.Quit;
WordApp := Unassigned;


All what you need is to add ActiveX, ComObj units in use-clause and declare
such consts and variables:
const 

  
wdFindContinue = 1;
  wdReplaceOne = 1;
  wdReplaceAll = 2;

var
  
WordApp: Variant;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







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