简体   繁体   English

如何使用delphi在Word模板中的ID之后添加文本?

[英]how can I add text after an id in a word template with delphi?

I have a "template", a regular word file, and I need to insert text in specific places. 我有一个“模板”,一个常规的Word文件,并且需要在特定位置插入文本。 I adapted the code from here: 我从这里改编了代码:

http://www.swissdelphicenter.ch/torry/showcode.php?id=1304 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. 因此,我将一些标识符放在word文件中并使用了replace函数。 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. 简而言之,我需要一种找到id( #social )并将其替换为大文本的方法...我看过range函数,但不了解它是如何工作的。 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. 制作Word模板的更好方法是在需要以编程方式添加信息的地方插入书签。 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 . 在Word模板中,只需突出显示要变成书签的范围,然后使用Insert -> Bookmark或按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. 在这里,使用.InsertAfter将在书签之后添加文本。 You can also use any other Range methods or properties, for example R.Text := 'foo'; 您还可以使用任何其他Range方法或属性,例如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. 将书签名称存储在某种智能结构中很有用,具体取决于您如何决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM