Word.ContentControl class

Represents a content control. Content controls are bounded and potentially labeled regions in a document that serve as containers for specific types of content. Individual content controls may contain contents such as images, tables, or paragraphs of formatted text. Currently, only rich text content controls are supported.

[ API set: WordApi 1.1 ]

Extends
OfficeExtension.ClientObject

Properties

appearance

Gets or sets the appearance of the content control. The value can be 'BoundingBox', 'Tags', or 'Hidden'.

[ API set: WordApi 1.1 ]

cannotDelete

Gets or sets a value that indicates whether the user can delete the content control. Mutually exclusive with removeWhenEdited.

[ API set: WordApi 1.1 ]

cannotEdit

Gets or sets a value that indicates whether the user can edit the contents of the content control.

[ API set: WordApi 1.1 ]

color

Gets or sets the color of the content control. Color is specified in '#RRGGBB' format or by using the color name.

[ API set: WordApi 1.1 ]

contentControls

Gets the collection of content control objects in the content control. Read-only.

[ API set: WordApi 1.1 ]

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

font

Gets the text format of the content control. Use this to get and set font name, size, color, and other properties. Read-only.

[ API set: WordApi 1.1 ]

id

Gets an integer that represents the content control identifier. Read-only.

[ API set: WordApi 1.1 ]

inlinePictures

Gets the collection of inlinePicture objects in the content control. The collection does not include floating images. Read-only.

[ API set: WordApi 1.1 ]

lists

Gets the collection of list objects in the content control. Read-only.

[ API set: WordApi 1.3 ]

paragraphs

Get the collection of paragraph objects in the content control. Read-only.

[ API set: WordApi 1.1 ]

parentBody

Gets the parent body of the content control. Read-only.

[ API set: WordApi 1.3 ]

parentContentControl

Gets the content control that contains the content control. Throws if there isn't a parent content control. Read-only.

[ API set: WordApi 1.1 ]

parentContentControlOrNullObject

Gets the content control that contains the content control. Returns a null object if there isn't a parent content control. Read-only.

[ API set: WordApi 1.3 ]

parentTable

Gets the table that contains the content control. Throws if it is not contained in a table. Read-only.

[ API set: WordApi 1.3 ]

parentTableCell

Gets the table cell that contains the content control. Throws if it is not contained in a table cell. Read-only.

[ API set: WordApi 1.3 ]

parentTableCellOrNullObject

Gets the table cell that contains the content control. Returns a null object if it is not contained in a table cell. Read-only.

[ API set: WordApi 1.3 ]

parentTableOrNullObject

Gets the table that contains the content control. Returns a null object if it is not contained in a table. Read-only.

[ API set: WordApi 1.3 ]

placeholderText

Gets or sets the placeholder text of the content control. Dimmed text will be displayed when the content control is empty.

[ API set: WordApi 1.1 ]

removeWhenEdited

Gets or sets a value that indicates whether the content control is removed after it is edited. Mutually exclusive with cannotDelete.

[ API set: WordApi 1.1 ]

style

Gets or sets the style name for the content control. Use this property for custom styles and localized style names. To use the built-in styles that are portable between locales, see the "styleBuiltIn" property.

[ API set: WordApi 1.1 ]

styleBuiltIn

Gets or sets the built-in style name for the content control. Use this property for built-in styles that are portable between locales. To use custom styles or localized style names, see the "style" property.

[ API set: WordApi 1.3 ]

subtype

Gets the content control subtype. The subtype can be 'RichTextInline', 'RichTextParagraphs', 'RichTextTableCell', 'RichTextTableRow' and 'RichTextTable' for rich text content controls. Read-only.

[ API set: WordApi 1.3 ]

tables

Gets the collection of table objects in the content control. Read-only.

[ API set: WordApi 1.3 ]

tag

Gets or sets a tag to identify a content control.

[ API set: WordApi 1.1 ]

text

Gets the text of the content control. Read-only.

[ API set: WordApi 1.1 ]

title

Gets or sets the title for a content control.

[ API set: WordApi 1.1 ]

type

Gets the content control type. Only rich text content controls are supported currently. Read-only.

[ API set: WordApi 1.1 ]

Methods

clear()

Clears the contents of the content control. The user can perform the undo operation on the cleared content.

[ API set: WordApi 1.1 ]

delete(keepContent)

Deletes the content control and its content. If keepContent is set to true, the content is not deleted.

[ API set: WordApi 1.1 ]

getHtml()

Gets an HTML representation of the content control object. When rendered in a web page or HTML viewer, the formatting will be a close, but not exact, match to the formatting of the document. This method does not return the exact same HTML for the same document on different platforms (Windows, Mac, etc.). If you need exact fidelity, or consistency across platforms, use ContentControl.getOoxml() and convert the returned XML to HTML.

[ API set: WordApi 1.1 ]

getOoxml()

Gets the Office Open XML (OOXML) representation of the content control object.

[ API set: WordApi 1.1 ]

getRange(rangeLocation)

Gets the whole content control, or the starting or ending point of the content control, as a range.

[ API set: WordApi 1.3 ]

getRange(rangeLocationString)

Gets the whole content control, or the starting or ending point of the content control, as a range.

[ API set: WordApi 1.3 ]

getTextRanges(endingMarks, trimSpacing)

Gets the text ranges in the content control by using punctuation marks and/or other ending marks.

[ API set: WordApi 1.3 ]

insertBreak(breakType, insertLocation)

Inserts a break at the specified location in the main document. The insertLocation value can be 'Start', 'End', 'Before', or 'After'. This method cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

[ API set: WordApi 1.1 ]

insertBreak(breakTypeString, insertLocation)

Inserts a break at the specified location in the main document. The insertLocation value can be 'Start', 'End', 'Before', or 'After'. This method cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

[ API set: WordApi 1.1 ]

insertFileFromBase64(base64File, insertLocation)

Inserts a document into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertFileFromBase64(base64File, insertLocationString)

Inserts a document into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertHtml(html, insertLocation)

Inserts HTML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertHtml(html, insertLocationString)

Inserts HTML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertInlinePictureFromBase64(base64EncodedImage, insertLocation)

Inserts an inline picture into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.2 ]

insertInlinePictureFromBase64(base64EncodedImage, insertLocationString)

Inserts an inline picture into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.2 ]

insertOoxml(ooxml, insertLocation)

Inserts OOXML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertOoxml(ooxml, insertLocationString)

Inserts OOXML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertParagraph(paragraphText, insertLocation)

Inserts a paragraph at the specified location. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.1 ]

insertParagraph(paragraphText, insertLocationString)

Inserts a paragraph at the specified location. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.1 ]

insertTable(rowCount, columnCount, insertLocation, values)

Inserts a table with the specified number of rows and columns into, or next to, a content control. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.3 ]

insertTable(rowCount, columnCount, insertLocationString, values)

Inserts a table with the specified number of rows and columns into, or next to, a content control. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.3 ]

insertText(text, insertLocation)

Inserts text into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertText(text, insertLocationString)

Inserts text into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

search(searchText, searchOptions)

Performs a search with the specified SearchOptions on the scope of the content control object. The search results are a collection of range objects.

[ API set: WordApi 1.1 ]

select(selectionMode)

Selects the content control. This causes Word to scroll to the selection.

[ API set: WordApi 1.1 ]

select(selectionModeString)

Selects the content control. This causes Word to scroll to the selection.

[ API set: WordApi 1.1 ]

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

split(delimiters, multiParagraphs, trimDelimiters, trimSpacing)

Splits the content control into child ranges by using delimiters.

[ API set: WordApi 1.3 ]

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Word.ContentControl object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.ContentControlData) that contains shallow copies of any loaded child properties from the original object.

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you are using this object across ".sync" calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you needed to have added the object to the tracked object collection when the object was first created.

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You will need to call "context.sync()" before the memory release takes effect.

Events

onDataChanged

Occurs when data within the content control are changed. To get the new text, load this content control in the handler. To get the old text, do not load it.

[ API set: WordApi BETA (PREVIEW ONLY) ]

onDeleted

Occurs when the content control is deleted. Do not load this content control in the handler, otherwise you won't be able to get its original properties.

[ API set: WordApi BETA (PREVIEW ONLY) ]

onSelectionChanged

Occurs when selection within the content control is changed.

[ API set: WordApi BETA (PREVIEW ONLY) ]

Property Details

appearance

Gets or sets the appearance of the content control. The value can be 'BoundingBox', 'Tags', or 'Hidden'.

[ API set: WordApi 1.1 ]

appearance: Word.ContentControlAppearance | "BoundingBox" | "Tags" | "Hidden";

Property Value

Word.ContentControlAppearance | "BoundingBox" | "Tags" | "Hidden"

cannotDelete

Gets or sets a value that indicates whether the user can delete the content control. Mutually exclusive with removeWhenEdited.

[ API set: WordApi 1.1 ]

cannotDelete: boolean;

Property Value

boolean

cannotEdit

Gets or sets a value that indicates whether the user can edit the contents of the content control.

[ API set: WordApi 1.1 ]

cannotEdit: boolean;

Property Value

boolean

color

Gets or sets the color of the content control. Color is specified in '#RRGGBB' format or by using the color name.

[ API set: WordApi 1.1 ]

color: string;

Property Value

string

contentControls

Gets the collection of content control objects in the content control. Read-only.

[ API set: WordApi 1.1 ]

readonly contentControls: Word.ContentControlCollection;

Property Value

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

context: RequestContext;

Property Value

font

Gets the text format of the content control. Use this to get and set font name, size, color, and other properties. Read-only.

[ API set: WordApi 1.1 ]

readonly font: Word.Font;

Property Value

id

Gets an integer that represents the content control identifier. Read-only.

[ API set: WordApi 1.1 ]

readonly id: number;

Property Value

number

inlinePictures

Gets the collection of inlinePicture objects in the content control. The collection does not include floating images. Read-only.

[ API set: WordApi 1.1 ]

readonly inlinePictures: Word.InlinePictureCollection;

Property Value

lists

Gets the collection of list objects in the content control. Read-only.

[ API set: WordApi 1.3 ]

readonly lists: Word.ListCollection;

Property Value

paragraphs

Get the collection of paragraph objects in the content control. Read-only.

[ API set: WordApi 1.1 ]

readonly paragraphs: Word.ParagraphCollection;

Property Value

parentBody

Gets the parent body of the content control. Read-only.

[ API set: WordApi 1.3 ]

readonly parentBody: Word.Body;

Property Value

parentContentControl

Gets the content control that contains the content control. Throws if there isn't a parent content control. Read-only.

[ API set: WordApi 1.1 ]

readonly parentContentControl: Word.ContentControl;

Property Value

parentContentControlOrNullObject

Gets the content control that contains the content control. Returns a null object if there isn't a parent content control. Read-only.

[ API set: WordApi 1.3 ]

readonly parentContentControlOrNullObject: Word.ContentControl;

Property Value

parentTable

Gets the table that contains the content control. Throws if it is not contained in a table. Read-only.

[ API set: WordApi 1.3 ]

readonly parentTable: Word.Table;

Property Value

parentTableCell

Gets the table cell that contains the content control. Throws if it is not contained in a table cell. Read-only.

[ API set: WordApi 1.3 ]

readonly parentTableCell: Word.TableCell;

Property Value

parentTableCellOrNullObject

Gets the table cell that contains the content control. Returns a null object if it is not contained in a table cell. Read-only.

[ API set: WordApi 1.3 ]

readonly parentTableCellOrNullObject: Word.TableCell;

Property Value

parentTableOrNullObject

Gets the table that contains the content control. Returns a null object if it is not contained in a table. Read-only.

[ API set: WordApi 1.3 ]

readonly parentTableOrNullObject: Word.Table;

Property Value

placeholderText

Gets or sets the placeholder text of the content control. Dimmed text will be displayed when the content control is empty.

[ API set: WordApi 1.1 ]

placeholderText: string;

Property Value

string

removeWhenEdited

Gets or sets a value that indicates whether the content control is removed after it is edited. Mutually exclusive with cannotDelete.

[ API set: WordApi 1.1 ]

removeWhenEdited: boolean;

Property Value

boolean

style

Gets or sets the style name for the content control. Use this property for custom styles and localized style names. To use the built-in styles that are portable between locales, see the "styleBuiltIn" property.

[ API set: WordApi 1.1 ]

style: string;

Property Value

string

styleBuiltIn

Gets or sets the built-in style name for the content control. Use this property for built-in styles that are portable between locales. To use custom styles or localized style names, see the "style" property.

[ API set: WordApi 1.3 ]

styleBuiltIn: Word.Style | "Other" | "Normal" | "Heading1" | "Heading2" | "Heading3" | "Heading4" | "Heading5" | "Heading6" | "Heading7" | "Heading8" | "Heading9" | "Toc1" | "Toc2" | "Toc3" | "Toc4" | "Toc5" | "Toc6" | "Toc7" | "Toc8" | "Toc9" | "FootnoteText" | "Header" | "Footer" | "Caption" | "FootnoteReference" | "EndnoteReference" | "EndnoteText" | "Title" | "Subtitle" | "Hyperlink" | "Strong" | "Emphasis" | "NoSpacing" | "ListParagraph" | "Quote" | "IntenseQuote" | "SubtleEmphasis" | "IntenseEmphasis" | "SubtleReference" | "IntenseReference" | "BookTitle" | "Bibliography" | "TocHeading" | "TableGrid" | "PlainTable1" | "PlainTable2" | "PlainTable3" | "PlainTable4" | "PlainTable5" | "TableGridLight" | "GridTable1Light" | "GridTable1Light_Accent1" | "GridTable1Light_Accent2" | "GridTable1Light_Accent3" | "GridTable1Light_Accent4" | "GridTable1Light_Accent5" | "GridTable1Light_Accent6" | "GridTable2" | "GridTable2_Accent1" | "GridTable2_Accent2" | "GridTable2_Accent3" | "GridTable2_Accent4" | "GridTable2_Accent5" | "GridTable2_Accent6" | "GridTable3" | "GridTable3_Accent1" | "GridTable3_Accent2" | "GridTable3_Accent3" | "GridTable3_Accent4" | "GridTable3_Accent5" | "GridTable3_Accent6" | "GridTable4" | "GridTable4_Accent1" | "GridTable4_Accent2" | "GridTable4_Accent3" | "GridTable4_Accent4" | "GridTable4_Accent5" | "GridTable4_Accent6" | "GridTable5Dark" | "GridTable5Dark_Accent1" | "GridTable5Dark_Accent2" | "GridTable5Dark_Accent3" | "GridTable5Dark_Accent4" | "GridTable5Dark_Accent5" | "GridTable5Dark_Accent6" | "GridTable6Colorful" | "GridTable6Colorful_Accent1" | "GridTable6Colorful_Accent2" | "GridTable6Colorful_Accent3" | "GridTable6Colorful_Accent4" | "GridTable6Colorful_Accent5" | "GridTable6Colorful_Accent6" | "GridTable7Colorful" | "GridTable7Colorful_Accent1" | "GridTable7Colorful_Accent2" | "GridTable7Colorful_Accent3" | "GridTable7Colorful_Accent4" | "GridTable7Colorful_Accent5" | "GridTable7Colorful_Accent6" | "ListTable1Light" | "ListTable1Light_Accent1" | "ListTable1Light_Accent2" | "ListTable1Light_Accent3" | "ListTable1Light_Accent4" | "ListTable1Light_Accent5" | "ListTable1Light_Accent6" | "ListTable2" | "ListTable2_Accent1" | "ListTable2_Accent2" | "ListTable2_Accent3" | "ListTable2_Accent4" | "ListTable2_Accent5" | "ListTable2_Accent6" | "ListTable3" | "ListTable3_Accent1" | "ListTable3_Accent2" | "ListTable3_Accent3" | "ListTable3_Accent4" | "ListTable3_Accent5" | "ListTable3_Accent6" | "ListTable4" | "ListTable4_Accent1" | "ListTable4_Accent2" | "ListTable4_Accent3" | "ListTable4_Accent4" | "ListTable4_Accent5" | "ListTable4_Accent6" | "ListTable5Dark" | "ListTable5Dark_Accent1" | "ListTable5Dark_Accent2" | "ListTable5Dark_Accent3" | "ListTable5Dark_Accent4" | "ListTable5Dark_Accent5" | "ListTable5Dark_Accent6" | "ListTable6Colorful" | "ListTable6Colorful_Accent1" | "ListTable6Colorful_Accent2" | "ListTable6Colorful_Accent3" | "ListTable6Colorful_Accent4" | "ListTable6Colorful_Accent5" | "ListTable6Colorful_Accent6" | "ListTable7Colorful" | "ListTable7Colorful_Accent1" | "ListTable7Colorful_Accent2" | "ListTable7Colorful_Accent3" | "ListTable7Colorful_Accent4" | "ListTable7Colorful_Accent5" | "ListTable7Colorful_Accent6";

Property Value

Word.Style | "Other" | "Normal" | "Heading1" | "Heading2" | "Heading3" | "Heading4" | "Heading5" | "Heading6" | "Heading7" | "Heading8" | "Heading9" | "Toc1" | "Toc2" | "Toc3" | "Toc4" | "Toc5" | "Toc6" | "Toc7" | "Toc8" | "Toc9" | "FootnoteText" | "Header" | "Footer" | "Caption" | "FootnoteReference" | "EndnoteReference" | "EndnoteText" | "Title" | "Subtitle" | "Hyperlink" | "Strong" | "Emphasis" | "NoSpacing" | "ListParagraph" | "Quote" | "IntenseQuote" | "SubtleEmphasis" | "IntenseEmphasis" | "SubtleReference" | "IntenseReference" | "BookTitle" | "Bibliography" | "TocHeading" | "TableGrid" | "PlainTable1" | "PlainTable2" | "PlainTable3" | "PlainTable4" | "PlainTable5" | "TableGridLight" | "GridTable1Light" | "GridTable1Light_Accent1" | "GridTable1Light_Accent2" | "GridTable1Light_Accent3" | "GridTable1Light_Accent4" | "GridTable1Light_Accent5" | "GridTable1Light_Accent6" | "GridTable2" | "GridTable2_Accent1" | "GridTable2_Accent2" | "GridTable2_Accent3" | "GridTable2_Accent4" | "GridTable2_Accent5" | "GridTable2_Accent6" | "GridTable3" | "GridTable3_Accent1" | "GridTable3_Accent2" | "GridTable3_Accent3" | "GridTable3_Accent4" | "GridTable3_Accent5" | "GridTable3_Accent6" | "GridTable4" | "GridTable4_Accent1" | "GridTable4_Accent2" | "GridTable4_Accent3" | "GridTable4_Accent4" | "GridTable4_Accent5" | "GridTable4_Accent6" | "GridTable5Dark" | "GridTable5Dark_Accent1" | "GridTable5Dark_Accent2" | "GridTable5Dark_Accent3" | "GridTable5Dark_Accent4" | "GridTable5Dark_Accent5" | "GridTable5Dark_Accent6" | "GridTable6Colorful" | "GridTable6Colorful_Accent1" | "GridTable6Colorful_Accent2" | "GridTable6Colorful_Accent3" | "GridTable6Colorful_Accent4" | "GridTable6Colorful_Accent5" | "GridTable6Colorful_Accent6" | "GridTable7Colorful" | "GridTable7Colorful_Accent1" | "GridTable7Colorful_Accent2" | "GridTable7Colorful_Accent3" | "GridTable7Colorful_Accent4" | "GridTable7Colorful_Accent5" | "GridTable7Colorful_Accent6" | "ListTable1Light" | "ListTable1Light_Accent1" | "ListTable1Light_Accent2" | "ListTable1Light_Accent3" | "ListTable1Light_Accent4" | "ListTable1Light_Accent5" | "ListTable1Light_Accent6" | "ListTable2" | "ListTable2_Accent1" | "ListTable2_Accent2" | "ListTable2_Accent3" | "ListTable2_Accent4" | "ListTable2_Accent5" | "ListTable2_Accent6" | "ListTable3" | "ListTable3_Accent1" | "ListTable3_Accent2" | "ListTable3_Accent3" | "ListTable3_Accent4" | "ListTable3_Accent5" | "ListTable3_Accent6" | "ListTable4" | "ListTable4_Accent1" | "ListTable4_Accent2" | "ListTable4_Accent3" | "ListTable4_Accent4" | "ListTable4_Accent5" | "ListTable4_Accent6" | "ListTable5Dark" | "ListTable5Dark_Accent1" | "ListTable5Dark_Accent2" | "ListTable5Dark_Accent3" | "ListTable5Dark_Accent4" | "ListTable5Dark_Accent5" | "ListTable5Dark_Accent6" | "ListTable6Colorful" | "ListTable6Colorful_Accent1" | "ListTable6Colorful_Accent2" | "ListTable6Colorful_Accent3" | "ListTable6Colorful_Accent4" | "ListTable6Colorful_Accent5" | "ListTable6Colorful_Accent6" | "ListTable7Colorful" | "ListTable7Colorful_Accent1" | "ListTable7Colorful_Accent2" | "ListTable7Colorful_Accent3" | "ListTable7Colorful_Accent4" | "ListTable7Colorful_Accent5" | "ListTable7Colorful_Accent6"

subtype

Gets the content control subtype. The subtype can be 'RichTextInline', 'RichTextParagraphs', 'RichTextTableCell', 'RichTextTableRow' and 'RichTextTable' for rich text content controls. Read-only.

[ API set: WordApi 1.3 ]

readonly subtype: Word.ContentControlType | "Unknown" | "RichTextInline" | "RichTextParagraphs" | "RichTextTableCell" | "RichTextTableRow" | "RichTextTable" | "PlainTextInline" | "PlainTextParagraph" | "Picture" | "BuildingBlockGallery" | "CheckBox" | "ComboBox" | "DropDownList" | "DatePicker" | "RepeatingSection" | "RichText" | "PlainText";

Property Value

Word.ContentControlType | "Unknown" | "RichTextInline" | "RichTextParagraphs" | "RichTextTableCell" | "RichTextTableRow" | "RichTextTable" | "PlainTextInline" | "PlainTextParagraph" | "Picture" | "BuildingBlockGallery" | "CheckBox" | "ComboBox" | "DropDownList" | "DatePicker" | "RepeatingSection" | "RichText" | "PlainText"

tables

Gets the collection of table objects in the content control. Read-only.

[ API set: WordApi 1.3 ]

readonly tables: Word.TableCollection;

Property Value

tag

Gets or sets a tag to identify a content control.

[ API set: WordApi 1.1 ]

tag: string;

Property Value

string

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy range object for the current selection.
    var range = context.document.getSelection();
    
    // Queue a command to create the content control.
    var myContentControl = range.insertContentControl();
    myContentControl.tag = 'Customer-Address';
    myContentControl.title = ' has t';
    myContentControl.style = 'Heading 2';
    myContentControl.insertText('One Microsoft Way, Redmond, WA 98052', 'Replace');
    myContentControl.cannotEdit = true;
    
    // Queue a command to load the id property for the content control you created.
    context.load(myContentControl, 'id');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log('Created content control with id: ' + myContentControl.id);
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});
// Traverses each paragraph of the document and wraps a content control on each with either a even or odd tags.
await Word.run(async (context) => {
  let paragraphs = context.document.body.paragraphs;
  paragraphs.load("$none"); // Don't need any properties; just wrap each paragraph with a content control.

  await context.sync();

  for (let i = 0; i < paragraphs.items.length; i++) {
    let contentControl = paragraphs.items[i].insertContentControl();
    // For even, tag "even".
    if (i % 2 === 0) {
      contentControl.tag = "even";
    } else {
      contentControl.tag = "odd";
    }
  }
  console.log("Content controls inserted: " + paragraphs.items.length);

  await context.sync();
});

text

Gets the text of the content control. Read-only.

[ API set: WordApi 1.1 ]

readonly text: string;

Property Value

string

title

Gets or sets the title for a content control.

[ API set: WordApi 1.1 ]

title: string;

Property Value

string

type

Gets the content control type. Only rich text content controls are supported currently. Read-only.

[ API set: WordApi 1.1 ]

readonly type: Word.ContentControlType | "Unknown" | "RichTextInline" | "RichTextParagraphs" | "RichTextTableCell" | "RichTextTableRow" | "RichTextTable" | "PlainTextInline" | "PlainTextParagraph" | "Picture" | "BuildingBlockGallery" | "CheckBox" | "ComboBox" | "DropDownList" | "DatePicker" | "RepeatingSection" | "RichText" | "PlainText";

Property Value

Word.ContentControlType | "Unknown" | "RichTextInline" | "RichTextParagraphs" | "RichTextTableCell" | "RichTextTableRow" | "RichTextTable" | "PlainTextInline" | "PlainTextParagraph" | "Picture" | "BuildingBlockGallery" | "CheckBox" | "ComboBox" | "DropDownList" | "DatePicker" | "RepeatingSection" | "RichText" | "PlainText"

Method Details

clear()

Clears the contents of the content control. The user can perform the undo operation on the cleared content.

[ API set: WordApi 1.1 ]

clear(): void;

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the content controls collection.
    contentControls.load('text');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        
        if (contentControls.items.length === 0) {
            console.log("There isn't a content control in this document.");
        } else {
            
            // Queue a command to clear the contents of the first content control.
            contentControls.items[0].clear();
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                console.log('Content control cleared of contents.');
            });      
        }
            
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

delete(keepContent)

Deletes the content control and its content. If keepContent is set to true, the content is not deleted.

[ API set: WordApi 1.1 ]

delete(keepContent: boolean): void;

Parameters

keepContent
boolean

Required. Indicates whether the content should be deleted with the content control. If keepContent is set to true, the content is not deleted.

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the content controls collection.
    contentControls.load('text');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        
        if (contentControls.items.length === 0) {
            console.log("There isn't a content control in this document.");
        } else {
            
            // Queue a command to delete the first content control. The
            // contents will remain in the document.
            contentControls.items[0].delete(true);
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                console.log('Content control cleared of contents.');
            });      
        }
            
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

getHtml()

Gets an HTML representation of the content control object. When rendered in a web page or HTML viewer, the formatting will be a close, but not exact, match to the formatting of the document. This method does not return the exact same HTML for the same document on different platforms (Windows, Mac, etc.). If you need exact fidelity, or consistency across platforms, use ContentControl.getOoxml() and convert the returned XML to HTML.

[ API set: WordApi 1.1 ]

getHtml(): OfficeExtension.ClientResult<string>;

Returns

OfficeExtension.ClientResult<string>

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection that contains a specific tag.
    var contentControlsWithTag = context.document.contentControls.getByTag('Customer-Address');
    
    // Queue a command to load the tag property for all of content controls. 
    context.load(contentControlsWithTag, 'tag');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControlsWithTag.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to get the HTML contents of the first content control.
            var html = contentControlsWithTag.items[0].getHtml();
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Content control HTML: ' + html.value);
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

getOoxml()

Gets the Office Open XML (OOXML) representation of the content control object.

[ API set: WordApi 1.1 ]

getOoxml(): OfficeExtension.ClientResult<string>;

Returns

OfficeExtension.ClientResult<string>

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of the content controls. 
    context.load(contentControls, 'id');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to get the OOXML contents of the first content control.
            var ooxml = contentControls.items[0].getOoxml();
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Content control OOXML: ' + ooxml.value);
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

getRange(rangeLocation)

Gets the whole content control, or the starting or ending point of the content control, as a range.

[ API set: WordApi 1.3 ]

getRange(rangeLocation?: Word.RangeLocation): Word.Range;

Parameters

rangeLocation
Word.RangeLocation

Optional. The range location can be 'Whole', 'Before', 'Start', 'End', 'After', or 'Content'.

Returns

getRange(rangeLocationString)

Gets the whole content control, or the starting or ending point of the content control, as a range.

[ API set: WordApi 1.3 ]

getRange(rangeLocationString?: "Whole" | "Start" | "End" | "Before" | "After" | "Content"): Word.Range;

Parameters

rangeLocationString
"Whole" | "Start" | "End" | "Before" | "After" | "Content"

Optional. The range location can be 'Whole', 'Before', 'Start', 'End', 'After', or 'Content'.

Returns

getTextRanges(endingMarks, trimSpacing)

Gets the text ranges in the content control by using punctuation marks and/or other ending marks.

[ API set: WordApi 1.3 ]

getTextRanges(endingMarks: string[], trimSpacing?: boolean): Word.RangeCollection;

Parameters

endingMarks
string[]

Required. The punctuation marks and/or other ending marks as an array of strings.

trimSpacing
boolean

Optional. Indicates whether to trim spacing characters (spaces, tabs, column breaks, and paragraph end marks) from the start and end of the ranges returned in the range collection. Default is false which indicates that spacing characters at the start and end of the ranges are included in the range collection.

Returns

insertBreak(breakType, insertLocation)

Inserts a break at the specified location in the main document. The insertLocation value can be 'Start', 'End', 'Before', or 'After'. This method cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

[ API set: WordApi 1.1 ]

insertBreak(breakType: Word.BreakType, insertLocation: Word.InsertLocation): void;

Parameters

breakType
Word.BreakType

Required. Type of break.

insertLocation
Word.InsertLocation

Required. The value can be 'Start', 'End', 'Before', or 'After'.

Returns

void

insertBreak(breakTypeString, insertLocation)

Inserts a break at the specified location in the main document. The insertLocation value can be 'Start', 'End', 'Before', or 'After'. This method cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

[ API set: WordApi 1.1 ]

insertBreak(breakTypeString: "Page" | "Next" | "SectionNext" | "SectionContinuous" | "SectionEven" | "SectionOdd" | "Line", insertLocation: "Before" | "After" | "Start" | "End" | "Replace"): void;

Parameters

breakTypeString
"Page" | "Next" | "SectionNext" | "SectionContinuous" | "SectionEven" | "SectionOdd" | "Line"

Required. Type of break.

insertLocation
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Start', 'End', 'Before', or 'After'.

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of content controls. 
    context.load(contentControls, 'id');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion. We now will have 
    // access to the content control collection.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to insert a page break after the first content control. 
            contentControls.items[0].insertBreak('page', "After");
            
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion. 
            return context.sync()
                .then(function () {
                    console.log('Inserted a page break after the first content control.');    
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

insertFileFromBase64(base64File, insertLocation)

Inserts a document into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertFileFromBase64(base64File: string, insertLocation: Word.InsertLocation): Word.Range;

Parameters

base64File
string

Required. The base64 encoded content of a .docx file.

insertLocation
Word.InsertLocation

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

insertFileFromBase64(base64File, insertLocationString)

Inserts a document into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertFileFromBase64(base64File: string, insertLocationString: "Before" | "After" | "Start" | "End" | "Replace"): Word.Range;

Parameters

base64File
string

Required. The base64 encoded content of a .docx file.

insertLocationString
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

insertHtml(html, insertLocation)

Inserts HTML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertHtml(html: string, insertLocation: Word.InsertLocation): Word.Range;

Parameters

html
string

Required. The HTML to be inserted in to the content control.

insertLocation
Word.InsertLocation

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

insertHtml(html, insertLocationString)

Inserts HTML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertHtml(html: string, insertLocationString: "Before" | "After" | "Start" | "End" | "Replace"): Word.Range;

Parameters

html
string

Required. The HTML to be inserted in to the content control.

insertLocationString
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of the content controls. 
    context.load(contentControls, 'id');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to put HTML into the contents of the first content control.
            contentControls.items[0].insertHtml(
                '<strong>HTML content inserted into the content control.</strong>',
                'Start');
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Inserted HTML in the first content control.');
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

insertInlinePictureFromBase64(base64EncodedImage, insertLocation)

Inserts an inline picture into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.2 ]

insertInlinePictureFromBase64(base64EncodedImage: string, insertLocation: Word.InsertLocation): Word.InlinePicture;

Parameters

base64EncodedImage
string

Required. The base64 encoded image to be inserted in the content control.

insertLocation
Word.InsertLocation

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

insertInlinePictureFromBase64(base64EncodedImage, insertLocationString)

Inserts an inline picture into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.2 ]

insertInlinePictureFromBase64(base64EncodedImage: string, insertLocationString: "Before" | "After" | "Start" | "End" | "Replace"): Word.InlinePicture;

Parameters

base64EncodedImage
string

Required. The base64 encoded image to be inserted in the content control.

insertLocationString
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

insertOoxml(ooxml, insertLocation)

Inserts OOXML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertOoxml(ooxml: string, insertLocation: Word.InsertLocation): Word.Range;

Parameters

ooxml
string

Required. The OOXML to be inserted in to the content control.

insertLocation
Word.InsertLocation

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

insertOoxml(ooxml, insertLocationString)

Inserts OOXML into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertOoxml(ooxml: string, insertLocationString: "Before" | "After" | "Start" | "End" | "Replace"): Word.Range;

Parameters

ooxml
string

Required. The OOXML to be inserted in to the content control.

insertLocationString
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of the content controls. 
    context.load(contentControls, 'id');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to put OOXML into the contents of the first content control.
            contentControls.items[0].insertOoxml("<pkg:package xmlns:pkg='http://schemas.microsoft.com/office/2006/xmlPackage'><pkg:part pkg:name='/_rels/.rels' pkg:contentType='application/vnd.openxmlformats-package.relationships+xml' pkg:padding='512'><pkg:xmlData><Relationships xmlns='http://schemas.openxmlformats.org/package/2006/relationships'><Relationship Id='rId1' Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' Target='word/document.xml'/></Relationships></pkg:xmlData></pkg:part><pkg:part pkg:name='/word/document.xml' pkg:contentType='application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'><pkg:xmlData><w:document xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' ><w:body><w:p><w:pPr><w:spacing w:before='360' w:after='0' w:line='480' w:lineRule='auto'/><w:rPr><w:color w:val='70AD47' w:themeColor='accent6'/><w:sz w:val='28'/></w:rPr></w:pPr><w:r><w:rPr><w:color w:val='70AD47' w:themeColor='accent6'/><w:sz w:val='28'/></w:rPr><w:t>This text has formatting directly applied to achieve its font size, color, line spacing, and paragraph spacing.</w:t></w:r></w:p></w:body></w:document></pkg:xmlData></pkg:part></pkg:package>", "End");
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Inserted OOXML in the first content control.');
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

// Read "Create better add-ins for Word with Office Open XML" for guidance on working with OOXML.
// https://docs.microsoft.com/office/dev/add-ins/word/create-better-add-ins-for-word-with-office-open-xml

insertParagraph(paragraphText, insertLocation)

Inserts a paragraph at the specified location. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.1 ]

insertParagraph(paragraphText: string, insertLocation: Word.InsertLocation): Word.Paragraph;

Parameters

paragraphText
string

Required. The paragraph text to be inserted.

insertLocation
Word.InsertLocation

Required. The value can be 'Start', 'End', 'Before', or 'After'. 'Before' and 'After' cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

Returns

insertParagraph(paragraphText, insertLocationString)

Inserts a paragraph at the specified location. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.1 ]

insertParagraph(paragraphText: string, insertLocationString: "Before" | "After" | "Start" | "End" | "Replace"): Word.Paragraph;

Parameters

paragraphText
string

Required. The paragraph text to be inserted.

insertLocationString
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Start', 'End', 'Before', or 'After'. 'Before' and 'After' cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

Returns

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of the content controls. 
    context.load(contentControls, 'id');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to insert a paragraph after the first content control. 
            contentControls.items[0].insertParagraph('Text of the inserted paragraph.', 'After');
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Inserted a paragraph after the first content control.');
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

insertTable(rowCount, columnCount, insertLocation, values)

Inserts a table with the specified number of rows and columns into, or next to, a content control. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.3 ]

insertTable(rowCount: number, columnCount: number, insertLocation: Word.InsertLocation, values?: string[][]): Word.Table;

Parameters

rowCount
number

Required. The number of rows in the table.

columnCount
number

Required. The number of columns in the table.

insertLocation
Word.InsertLocation

Required. The value can be 'Start', 'End', 'Before', or 'After'. 'Before' and 'After' cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

values
string[][]

Optional 2D array. Cells are filled if the corresponding strings are specified in the array.

Returns

insertTable(rowCount, columnCount, insertLocationString, values)

Inserts a table with the specified number of rows and columns into, or next to, a content control. The insertLocation value can be 'Start', 'End', 'Before', or 'After'.

[ API set: WordApi 1.3 ]

insertTable(rowCount: number, columnCount: number, insertLocationString: "Before" | "After" | "Start" | "End" | "Replace", values?: string[][]): Word.Table;

Parameters

rowCount
number

Required. The number of rows in the table.

columnCount
number

Required. The number of columns in the table.

insertLocationString
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Start', 'End', 'Before', or 'After'. 'Before' and 'After' cannot be used with 'RichTextTable', 'RichTextTableRow' and 'RichTextTableCell' content controls.

values
string[][]

Optional 2D array. Cells are filled if the corresponding strings are specified in the array.

Returns

insertText(text, insertLocation)

Inserts text into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertText(text: string, insertLocation: Word.InsertLocation): Word.Range;

Parameters

text
string

Required. The text to be inserted in to the content control.

insertLocation
Word.InsertLocation

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

insertText(text, insertLocationString)

Inserts text into the content control at the specified location. The insertLocation value can be 'Replace', 'Start', or 'End'.

[ API set: WordApi 1.1 ]

insertText(text: string, insertLocationString: "Before" | "After" | "Start" | "End" | "Replace"): Word.Range;

Parameters

text
string

Required. The text to be inserted in to the content control.

insertLocationString
"Before" | "After" | "Start" | "End" | "Replace"

Required. The value can be 'Replace', 'Start', or 'End'. 'Replace' cannot be used with 'RichTextTable' and 'RichTextTableRow' content controls.

Returns

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of the content controls. 
    context.load(contentControls, 'id');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to replace text in the first content control. 
            contentControls.items[0].insertText('Replaced text in the first content control.', 'Replace');
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Replaced text in the first content control.');
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

// The Silly stories add-in sample shows how to use the insertText method.
// https://aka.ms/sillystorywordaddin

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(option?: Word.Interfaces.ContentControlLoadOptions): Word.ContentControl;

Parameters

Returns

Remarks

In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Word.ContentControl - Where option is a comma-delimited string or an array of strings that specify the properties to load.

load(option?: { select?: string; expand?: string; }): Word.ContentControl - Where option.select is a comma-delimited string that specifies the properties to load, and options.expand is a comma-delimited string that specifies the navigation properties to load.

load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Word.ContentControl - Only available on collection types. It is similar to the preceding signature. Option.top specifies the maximum number of collection items that can be included in the result. Option.skip specifies the number of items that are to be skipped and not included in the result. If option.top is specified, the result set will start after skipping the specified number of items.

Examples

// Load all of the content control properties
// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of the content controls. 
    context.load(contentControls, 'id');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to load the properties on the first content control. 
            contentControls.items[0].load(  'appearance,' +
                                            'cannotDelete,' +
                                            'cannotEdit,' +
                                            'id,' +
                                            'placeHolderText,' +
                                            'removeWhenEdited,' +
                                            'title,' +
                                            'text,' +
                                            'type,' +
                                            'style,' +
                                            'tag,' +
                                            'font/size,' +
                                            'font/name,' +
                                            'font/color');             
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Property values of the first content control:' + 
                        '   ----- appearance: ' + contentControls.items[0].appearance + 
                        '   ----- cannotDelete: ' + contentControls.items[0].cannotDelete +
                        '   ----- cannotEdit: ' + contentControls.items[0].cannotEdit +
                        '   ----- color: ' + contentControls.items[0].color +
                        '   ----- id: ' + contentControls.items[0].id +
                        '   ----- placeHolderText: ' + contentControls.items[0].placeholderText +
                        '   ----- removeWhenEdited: ' + contentControls.items[0].removeWhenEdited +
                        '   ----- title: ' + contentControls.items[0].title +
                        '   ----- text: ' + contentControls.items[0].text +
                        '   ----- type: ' + contentControls.items[0].type +
                        '   ----- style: ' + contentControls.items[0].style +
                        '   ----- tag: ' + contentControls.items[0].tag +
                        '   ----- font size: ' + contentControls.items[0].font.size +
                        '   ----- font name: ' + contentControls.items[0].font.name +
                        '   ----- font color: ' + contentControls.items[0].font.color);
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames?: string | string[]): Word.ContentControl;

Parameters

propertyNames
string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths?: { select?: string; expand?: string; }): Word.ContentControl;

Parameters

propertyNamesAndPaths
{ select?: string; expand?: string; }

Where propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

Returns

search(searchText, searchOptions)

Performs a search with the specified SearchOptions on the scope of the content control object. The search results are a collection of range objects.

[ API set: WordApi 1.1 ]

search(searchText: string, searchOptions?: Word.SearchOptions | {
            ignorePunct?: boolean;
            ignoreSpace?: boolean;
            matchCase?: boolean;
            matchPrefix?: boolean;
            matchSuffix?: boolean;
            matchWholeWord?: boolean;
            matchWildcards?: boolean;
        }): Word.RangeCollection;

Parameters

searchText
string

Required. The search text.

searchOptions
Word.SearchOptions | { ignorePunct?: boolean; ignoreSpace?: boolean; matchCase?: boolean; matchPrefix?: boolean; matchSuffix?: boolean; matchWholeWord?: boolean; matchWildcards?: boolean; }

Optional. Options for the search.

Returns

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
    
    // Create a proxy object for the content controls collection.
    var contentControls = context.document.contentControls;
    
    // Queue a command to load the id property for all of the content controls. 
    context.load(contentControls, 'id');
     
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            // Queue a command to select the first content control.
            contentControls.items[0].select();
        
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    console.log('Selected the first content control.');
            });
        }
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

select(selectionMode)

Selects the content control. This causes Word to scroll to the selection.

[ API set: WordApi 1.1 ]

select(selectionMode?: Word.SelectionMode): void;

Parameters

selectionMode
Word.SelectionMode

Optional. The selection mode can be 'Select', 'Start', or 'End'. 'Select' is the default.

Returns

void

select(selectionModeString)

Selects the content control. This causes Word to scroll to the selection.

[ API set: WordApi 1.1 ]

select(selectionModeString?: "Select" | "Start" | "End"): void;

Parameters

selectionModeString
"Select" | "Start" | "End"

Optional. The selection mode can be 'Select', 'Start', or 'End'. 'Select' is the default.

Returns

void

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties: Interfaces.ContentControlUpdateData, options?: OfficeExtension.UpdateOptions): void;

Parameters

properties
Interfaces.ContentControlUpdateData

A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.

options
OfficeExtension.UpdateOptions

Provides an option to suppress errors if the properties object tries to set any read-only properties.

Returns

void

Remarks

This method has the following additional signature:

set(properties: Word.ContentControl): void

Examples

// Adds title and colors to odd and even content controls and changes their appearance.
await Word.run(async (context) => {
  // Gets the complete sentence (as range) associated with the insertion point.
  let evenContentControls = context.document.contentControls.getByTag("even");
  let oddContentControls = context.document.contentControls.getByTag("odd");
  evenContentControls.load("length");
  oddContentControls.load("length");

  await context.sync();

  for (let i = 0; i < evenContentControls.items.length; i++) {
    // Change a few properties and append a paragraph
    evenContentControls.items[i].set({
      color: "red",
      title: "Odd ContentControl #" + (i + 1),
      appearance: "Tags"
    });
    evenContentControls.items[i].insertParagraph("This is an odd content control", "End");
  }

  for (let j = 0; j < oddContentControls.items.length; j++) {
    // Change a few properties and append a paragraph
    oddContentControls.items[j].set({
      color: "green",
      title: "Even ContentControl #" + (j + 1),
      appearance: "Tags"
    });
    oddContentControls.items[j].insertHtml("This is an <b>even</b> content control", "End");
  }

  await context.sync();
});

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

set(properties: Word.ContentControl): void;

Parameters

properties
Word.ContentControl

Returns

void

split(delimiters, multiParagraphs, trimDelimiters, trimSpacing)

Splits the content control into child ranges by using delimiters.

[ API set: WordApi 1.3 ]

split(delimiters: string[], multiParagraphs?: boolean, trimDelimiters?: boolean, trimSpacing?: boolean): Word.RangeCollection;

Parameters

delimiters
string[]

Required. The delimiters as an array of strings.

multiParagraphs
boolean

Optional. Indicates whether a returned child range can cover multiple paragraphs. Default is false which indicates that the paragraph boundaries are also used as delimiters.

trimDelimiters
boolean

Optional. Indicates whether to trim delimiters from the ranges in the range collection. Default is false which indicates that the delimiters are included in the ranges returned in the range collection.

trimSpacing
boolean

Optional. Indicates whether to trim spacing characters (spaces, tabs, column breaks, and paragraph end marks) from the start and end of the ranges returned in the range collection. Default is false which indicates that spacing characters at the start and end of the ranges are included in the range collection.

Returns

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Word.ContentControl object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.ContentControlData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Word.Interfaces.ContentControlData;

Returns

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you are using this object across ".sync" calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you needed to have added the object to the tracked object collection when the object was first created.

track(): Word.ContentControl;

Returns

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You will need to call "context.sync()" before the memory release takes effect.

untrack(): Word.ContentControl;

Returns

Event Details

onDataChanged

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Occurs when data within the content control are changed. To get the new text, load this content control in the handler. To get the old text, do not load it.

[ API set: WordApi BETA (PREVIEW ONLY) ]

readonly onDataChanged: OfficeExtension.EventHandlers<Word.ContentControlEventArgs>;

Returns

OfficeExtension.EventHandlers<Word.ContentControlEventArgs>

onDeleted

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Occurs when the content control is deleted. Do not load this content control in the handler, otherwise you won't be able to get its original properties.

[ API set: WordApi BETA (PREVIEW ONLY) ]

readonly onDeleted: OfficeExtension.EventHandlers<Word.ContentControlEventArgs>;

Returns

OfficeExtension.EventHandlers<Word.ContentControlEventArgs>

onSelectionChanged

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Occurs when selection within the content control is changed.

[ API set: WordApi BETA (PREVIEW ONLY) ]

readonly onSelectionChanged: OfficeExtension.EventHandlers<Word.ContentControlEventArgs>;

Returns

OfficeExtension.EventHandlers<Word.ContentControlEventArgs>