Reputation: 2702
Is it possible to programattically define a Range as a ContentControl in Word using Delphi?
We have a number of templates which have boiler plate text inserted depending on the choices a user makes in a Delphi application. 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.
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?
Upvotes: 1
Views: 391
Reputation: 2702
The following replaces a bookmark in a Word Template with a Rich Text Content Control, displaying the required placeholder text
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;
Upvotes: 2