I have a "template", a regular word file, and I need to insert text in specific places. I adapted the code from here:
http://www.swissdelphicenter.ch/torry/showcode.php?id=1304
to fit my needs. So I put some identifiers in the word file and used the replace function. It works fine but i can't do that to insert the text of a memo cause it's too big for the word replace function...
In short, i need a way to find an id (#social
) and replace that with a large text... I've seen the range function but dont understand how it works. I need an example to get an idea of how to do it, please.
A better way to do Word templates is to insert bookmarks wherever information needs to be added programmatically. In the Word template, simply highlight the range you wish to turn into a bookmark and either use Insert -> Bookmark
or press Ctrl+Shift+F5.
Give the bookmark a name and then insert your text like:
var
LWordDoc : WordDocument;
R : WordRange;
// ...open the document, etc
if LWordDoc.Bookmarks.Exists('My Bookmark') then begin
R := LWordDoc.Bookmarks.Item('My Bookmark').Range;
R.InsertAfter('foo');
end else begin
// handle missinng bookmark
end;
Here, using .InsertAfter
the text will be added after the bookmark. You can also use any other Range
methods or properties, for example R.Text := 'foo';
to substitute the highlighted range with the text you supply.
It is useful to store your bookmark names in some sort of intelligent structure - how you decide to do that is up to you.