Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to search and replace text in a Word document Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Nov-02
Category
OLE
Language
Delphi 5.x
Views
176
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How to search and replace text in a Word document

Answer:

Solve 1:

You should use a variant because the Find.Execute method is a bit buggy. Something 
like this, for example:

1   { ... }
2   var
3     Rnge: OleVariant;
4   { ... }
5   
6   Rnge := Doc.Content;
7   Rnge.Find.Execute('old', Wrap := wdFindContinue, ReplaceWith := 'new', Replace :=
8     wdReplaceAll);
9   { ... }



Solve 2:

10  { ... }
11    { Create the OLE Object }
12  WordApp := CreateOLEObject('Word.Application');
13  WordApp.Documents.Open(yourDocFile);
14  WordApp.Selection.Find.ClearFormatting;
15  WordApp.Selection.Find.Text := yourOldStr;
16  WordApp.Selection.Find.Replacement.Text := yourNewStr;
17  WordApp.Selection.Find.Forward := True;
18  WordApp.Selection.Find.Wrap := 1; {wdFindContinue}
19  WordApp.Selection.Find.Format := False;
20  WordApp.Selection.Find.MatchCase := False;
21  WordApp.Selection.Find.MatchWholeWord := False;
22  WordApp.Selection.Find.MatchWildcards := True;
23  WordApp.Selection.Find.MatchSoundsLike := False;
24  WordApp.Selection.Find.MatchAllWordForms := False;
25  WordApp.Selection.Find.Execute(Replace := 2); {wdReplaceAll}
26  {Or as alternative:  WordApp.Selection.Find.Execute(Replace := 1); for one replace}
27  WordApp.ActiveDocument.SaveAs(yourNewDocFile);
28  WordApp.Quit;
29  WordApp := Unassigned;
30  { ... }


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC