Link to home
Start Free TrialLog in
Avatar of rumpet
rumpet

asked on

How to Replace a text in MS Word using Delphi

Hi,

I'm making a program which must merge a MS Word doc file with a database fields.
I already know how to create new doc file and how to insert a text in it  from Delphi 4, but I need to know if there is a possibility to replace a text in an existing doc file.

For example <Name> must be replaced with 'Peter Johnson'
<Address> with 'Santa Monica blv'
and so on.

Thank you in advance
Roumen


Avatar of Epsylon
Epsylon

Are you using Outlook for names, addresses etc?

Here is some info on Outlook and Word:

http://www.progfa.com/delphi/faq/uddf/pages/msoffice.htm
Rumpet,

I'm assuming that the doc may be large and that your program will step through it line-by-line correct? If so read each line and pass it to the following function:

=========================================================
{ This function searches for the occurance of a substring
  (sToReplace) within another string (sOrg) and replaces
  it with (sReplaceWith).        The result is the
  origional string with the replacements made. If no
  replacements were made then the origional string is
  returned without modification.
}

function fnStrReplace(sOrg: String;
         sToReplace: String; sReplaceWith: String): String;
var kxIDX: Integer;
Begin
kxIDX := 0;
While Pos(sToReplace, sOrg) >kxIDX do
      Begin
      kxIDX := Pos(sToReplace,sOrg);
      Delete(sOrg,kxIDX,Length(sToReplace));
      Insert(sReplaceWith, sOrg, kxIDX);
      end;
Result := sOrg;
end;
=========================================================
An example would be

//Open file...
//read a line into "sLineString"

sLineString := fnStrReplace(sLineString, '<Name>', 'Peter Johnson')

Then write it back out to a secondary (temp) file, print it, whatever and when your done with all the lines in the document, print the temp file, dump it and do it all over again with the next name, address, etc.

Hope this helps and Good Luck with your project!

Pegasus
Avatar of rumpet

ASKER

Thank you Pegasus,
but if it was so easy to do it in this way I would have already done it.
The problem is that the doc file may have background color, pictures, text which is bold and so on.
If I simpy replace the lines and make a new doc file I'll lose the rest information
( pictures etc. )
I speak about  OLE automation, using the find & replace function of
Word. I think that the answer is in the VBA docs for Word, but I haven't got them.

I think that it is more clear now.
Thanks,
Roumen
 
Rumpet,

Understood. Your origional question did not reflect these things. Good luck with your project.

Pegasus
Are you trying to make a quasi newsletter writing and distribution program? If so its much easier to forget the word ole and just send your form to a browser and find/replace your text there and then print it.......if printing is your ultimate goal here which I assume it is due to the references to address.

Gabe
Hi, rumpet!
If you use Word 97 you can do anything you want.
You can get the procedures and functions by recording a macros and then opening the Visual Basic editor in Word you will get all your operations.
You can use the same functions in Delphi, f. e.:
...
  Word.Selection.MoveRight;
  Word.TypeText('Peter Johnson');
...

if you get questions with recording macros I'll help you.
And what's the main problem: to find the text or to replace it?
VBA is definately the answer here.  Word, unfortunately, does something to its files which does not allow them to be editted without corrupting them, therefore you need to edit them directly using a method which will write the Word format back to disk.

There used to be a program which was available _AGES_ ago for Delphi 1 which used Word documents for templates, but I can not for the life of me remember what it was (anyone?).

Short of that, Automation will do it, but it wouldnt be a job I would like to tackle at this stage.

Just as food for thought, I have written quite a few mail-merge applications, and the way I define a replacement field is to use some marker characters.  For instance, if I wanted to scan a document and replace a field with a name of someone, I would mark the replacement area in the original file like this <%@FIRSTNAME@%>.  The likelyhood of this exact string being inserted into a document is so unlikely, I doubt it would ever happen.

Sorry I cant help you any more than this at the moment.  It looks like a very interesting project!!

Stu.
ASKER CERTIFIED SOLUTION
Avatar of RBertora
RBertora
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I knew there are some function to merge document with database::MergePrint.
does it help?
RBertora has hit the nail right on the head!

RBertora.  Where do you find info like this?  Any books you can recommend to us all?

Cheers,

Stu.
Um, I don't know of any books, I use VBAWRD8.hlp, which ships with standard version of office 98.

With a bit of determination one can figure out the Delphi equivalent of the VB code in this file. Start with the
Application object and work from there.

Regards,
Bert.
Thanks for the tip, Bert.  I'll definately take a look!

Stu.
Cool its fairly powerfull stuff, but a bit slow, with com though you can interface to word (not 98 but 2000 I think)
in a more direct manner which should be a lot faster than the ole/variant method.. unfortunately I do not have the newest version of word, and cannot test the theory... any way this does fairly well for the moment.
hope rumpet finds it worth A grade, and you suceed with your stuff Stuart.
Hi, Stu!
Another tip:
In Word 97 press Alt+F11 - it will open Visual Basic Editor,
after that press F2 - and you will get all classes and functions.

This is fantastic!  Thanks heaps for this guys.  Although it is something that I have not really needed to do a lot of in the past, its definately something I will have to remember in the future!

Thanks again.

Stu.
Avatar of rumpet

ASKER

Thank you !!!
Thank you !!!
Thank you RBertora !!!
It's EXACTLY what I'm looking for.
And It WORKS.
You deserve your points.
Now I can finish my project.

Thanks again,
Roumen