delsql
delsql

Reputation: 651

Find a particular string in word document and update

I have to update some database field data from Delplhi to a word document. I have to find a particular field which repeats itself in several places in a word document(Table,Paragraph 1, paragraph 5).

wordapp: Twordapplication;
Bookmarkname,Bookmarkrange:Olevariant;
Bookmarkname := 'Supplier';
if Wordapp.ActiveDocument.Bookmarks.Exists(BookmarkName) then //condition to check for a particular string
begin
BookmarkRange := Wordapp.ActiveDocument.Bookmarks.Item(BookmarkName).Range;
BookmarkRange.InsertAfter('Database Value/String that needs to be updated in word document');

I am able to find a string and then place a bookmark beside it in word and update the value in the created bookmark using delphi code provided above. My problem is that, I have tried using the same bookmark name for in another place it doesn't work. Can someone help? Thanks in advance

Upvotes: 0

Views: 565

Answers (2)

Cindy Meister
Cindy Meister

Reputation: 25693

You can use three bookmarks with (slightly) different names.

Or you can use Content Controls instead of bookmarks. Content Controls can have the same Title or Tag (they have no "Name") property. The Document SelectionContentControlsByTitle (or ...ByTag) returns an array of all the content controls in the document, then you can loop the array and write to them.

Even more efficient, especially for data purposes, would be to link the content controls to a Custom XML Part. Multiple content controls can be linked to the same XML node: writing to the node will update the data in all linked content controls.

Upvotes: 1

penarthur66
penarthur66

Reputation: 321

A book mark can only exist in one place, so you can't do a multiple replace using that method.

You can however do a straightforward search and replace in Word automation so you can use tags e.g. {{forename}}, {{address1}} or whatever and simply replace that text with values from your database.

Much easier to use than bookmarks since you can actually see the tags in the template document.

Upvotes: 0

Related Questions