简体   繁体   English

使用 Delphi 将单词范围替换为内容控件

[英]Replace a Word Range with a Content Control using Delphi

Is it possible to programattically define a Range as a ContentControl in Word using Delphi?是否可以使用 Delphi 在 Word 中以编程方式将 Range 定义为 ContentControl?

We have a number of templates which have boiler plate text inserted depending on the choices a user makes in a Delphi application.我们有许多模板,它们根据用户在 Delphi 应用程序中所做的选择插入了样板文本。 Those choice may also cause bookmarked ranges to be deleted.这些选择还可能导致已添加书签的范围被删除。

Our code for updating or deleting a bookmarked range is as follows:我们更新或删除书签范围的代码如下:

var
  R: WordRange;
  bookmark: OleVariant;
...
bookmark := 'bookmarkName';
R := MainFOrm.WordDoc.Bookmarks.Item(bookmark).Range;
if (addText = true) begin
  R.Text := 'This is the text to insert';
  R.HighlightColorIndex := wdTurquoise;
end else begin
  R.Delete(EmptyParam, EmptyParam);
end;
...

Ideally in the above example we would define the Range as a Rich Text Content Controls displaying the default text.理想情况下,在上面的示例中,我们将 Range 定义为显示默认文本的富文本内容控件。

This would be interspersed with other bookmarks as above.这将与上面的其他书签穿插。

Alternativly we could define the Rich Text Controls on the template and update their content / delete them as necessary?或者,我们可以在模板上定义富文本控件并根据需要更新它们的内容/删除它们?

The following replaces a bookmark in a Word Template with a Rich Text Content Control, displaying the required placeholder text下面将 Word 模板中的书签替换为富文本内容控件,显示所需的占位符文本

var
  bookmark: OleVariant;
  newCC: ContentControl;
  ccBlock: BuildingBlock;
  ccRange: WordRange;
  R: WordRange;
begin
  bookmark := 'bookmarkName';
  R := MainForm.WordDoc.Bookmarks.Item(bookmark).Range;
  // Clear any text in the template
  R.Text := '';
  // Create the new control
  newCC := MainForm.WordDoc.ContentControls.Add(wdContentControlRichText, R);
  // ccBlock and ccRange are optional but won't accept EmptyParam
  newCC.SetPlaceholderText(ccBlock, ccRange, Trim(Memo2.Text));
  // We can reuse ccRange to highlight the placeholder text. Defining earlier breaks setPlaceHolder
  ccRange := newCC.Range;
  ccRange.HighlightColorIndex := wdYellow;
end;

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

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