【问题标题】:Delphi: Set Word ContentControl to temporary programatticallyDelphi:以编程方式将 Word ContentControl 设置为临时
【发布时间】:2021-01-14 15:05:11
【问题描述】:

我正在使用 Delphi 10.4 将 ContentControl 插入 MS Word 文档

使用以下 sn-p 将内容控件插入到模板上的书签中:

procedure TBF.SetPI;
var
  R: WordRange;
  bookmark: OleVariant;
  s: string;
begin
  s:= 'insert text here';
  bookmark := 'textValue';
  R := MF.WordDoc.Bookmarks.Item(bookmark).Range;
  R.Text := s;
  R.HighlightColorIndex := wdYellow;
  MF.WordDoc.ContentControls.Add(wdContentControlRichText,R);
end;

这成功创建了 ContentControl,但是一旦编辑了文本,该控件就会保持不变。

Word 可以选择将 ContentControls 标记为“临时”,而 VBA 是

Selection.ParentContentControl.Temporary = True

Delphi 公开了与 R.ParentContentControl.Temporary 相同的内容,需要 WordBool 值。

尽我所能,我无法让 Delphi 接受 True 值并将其传递给 Word。

【问题讨论】:

    标签: delphi ms-word delphi-10.4-sydney


    【解决方案1】:

    问题是您试图将 Delphi 布尔值分配给 WordBool。

    Delphi 布尔值是 8 位(1 字节大小)。但 WordBool 实际上是 16 位的(大小为 2 字节)。

    有关此检查的更多信息System.WordBool;

    【讨论】:

    • 如果您还提供了问题的解决方案,这将是一个更好的答案。 :-)
    【解决方案2】:

    解决方案是我试图访问错误的 .ContentControl.Temporary 实例,而我还设置了 Range.Text 值。

    以下是上述示例的工作版本。

    procedure TBF.SetPI;
    var
      R: WordRange;
      bookmark: OleVariant;
      s: string;
      cc: ContentControl;
    begin
      s := 'insert text here';
      bookmark := 'textValue';
      R := MF.WordDoc.Bookmarks.Item(bookmark).Range;
      R.Text := '';
      cc := MF.WordDoc.ContentControls.Add(wdContentControlRichText, R);
      cc.SetPlaceholderText(nil, nil, s);
      cc.Range.HighlightColorIndex := wdYellow;
      cc.Temporary := true;
    end;
    

    【讨论】:

      猜你喜欢
      • 2012-11-08
      • 1970-01-01
      • 2018-02-02
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2011-12-02
      相关资源
      最近更新 更多