This topic describes how to obtain the number of controls, set the properties of a control, obtain the title of a control, and obtain the tag of a control when you use a text document.
Obtain content control objects
Obtain all content control objects in a document.
Syntax
Expression.ActiveDocument.ContentControlsExpression: the document type application object.
Example
//@file=base.docx async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; }
Obtain the number of content controls
Obtain the number of content controls in a document.
Syntax
Expression.ActiveDocument.ContentControls.CountExpression: the document type application object.
Return value
Returns the
numberwhich represents the number of content controls in the document.Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Obtain the number of content controls. const count = await contentControls.Count; console.log(count); }
A single content control
Obtain a single content control object
Syntax
Expression.ActiveDocument.ContentControls.Item(Index)Expression: the document type application object.
Parameters
Parameter
Type
Required
Description
Index
String
Yes
The ordinal position of the content control to return.
Return value
Returns the specified content control object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Obtain the first content control. const contentControl = await contentControls.Item(1); }
Obtain or set the text of a content control
Obtain or set the text of a content control by using Range.Text.
Only JS-SDK V1.1.15 and later support this feature.
Syntax
Expression.ActiveDocument.ContentControls.Item(Index).RangeExpression: the document type application object.
Example
Obtain the range of the content control
async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Obtain the first content control. const contentControl = await contentControls.Item(1); // Obtain the range of the first content control. const range = await contentControl.Range; console.log(range); }Obtain or set the text of a content control
async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Obtain the first content control. const contentControl = await contentControls.Item(1); // Obtain the range of the first content control. const range = await contentControl.Range; // Obtain the text of the first content control. const text = range.Text; console.log(text); // Set the text of the first content control. range.Text = 'Aliyun'; }
Obtain the placeholder text of a content control
Only JS-SDK V1.1.15 and later support this feature.
Syntax
Expression.ActiveDocument.ContentControls.Item(Index).PlaceholderTextExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Obtain the first content control. const contentControl = await contentControls.Item(1); // Obtain the placeholder text of the first content control. const placeholderText = await contentControl.PlaceholderText; console.log(placeholderText); }
Obtain the title of a content control
Syntax
Expression.ActiveDocument.ContentControls.Item(Index).TitleExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Obtain the first content control. const contentControl = await contentControls.Item(1); // Set the title of the first content control. contentControl.Title = 'Aliyun'; // Obtain the title of the first content control. const title = await contentControl.Title; console.log(title); }
Obtain or set the tag of a content control
Syntax
Expression.ActiveDocument.ContentControls.Item(Index).TagExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Obtain the first content control. const contentControl = await contentControls.Item(1); // Set the tag of the first content control. contentControl.Tag = 'Aliyun'; // Obtain the tag of the first content control. const Tag = await contentControl.Tag; console.log(Tag); }
Add a content control
Only JS-SDK V1.1.15 and later support this feature.
Syntax
Expression.ActiveDocument.ContentControls.Add()Expression: the document type application object.
Return value
Returns the new
ContentControlobject.Example
async function example() { await instance.ready(); const app = instance.Application; // Move the cursor to the front of the specified position. const info=await app.ActiveDocument.Find.Execute ('different'); const pos = info[0] && info[0].pos || 0; await app.ActiveDocument.Range.SetRange(pos, pos); // Obtain content control objects. const contentControls = await app.ActiveDocument.ContentControls; // Insert a content control at the position of the cursor. await contentControls.Add(); }