Skip to content

Latest commit

 

History

History
825 lines (536 loc) · 22.4 KB

File metadata and controls

825 lines (536 loc) · 22.4 KB

Body Object (JavaScript API for Word)

Represents the body of a document or a section.

Applies to: Office 2016 link to contentcontrolcollection

Property Type Description
style string Gets or sets the style used for the body. This is the name of the pre-installed or custom style.
text string Gets the text of the body. Use the insertText method to insert text. Read-only.

See property access examples.

Relationships

Relationship Type Description
contentControls ContentControlCollection Gets the collection of rich text content control objects that are in the body. Read-only.
Font Font Gets the text format of the body. Use this to get and set font name, size, color, and other properties. Read-only.
inlinePictures InlinePictureCollection Gets the collection of inlinePicture objects that are in the body. The collection does not include floating images. Read-only.
paragraphs ParagraphCollection Gets the collection of paragraph objects that are in the body. Read-only.
parentContentControl ContentControl Gets the content control that contains the body. Returns null if there isn't a parent content control. Read-only.
table    

Methods

Method Return Type Description
clear() void Clears the contents of the body object. The user can perform the undo operation on the cleared content.
getHtml() string Gets the HTML representation of the body object.
getOoxml() string Gets the OOXML (Office Open XML) representation of the body object.
insertBreak(breakType: BreakType, insertLocation: InsertLocation) void Inserts a break at the specified location. The insertLocation value can be 'Start' or 'End'.
insertContentControl() ContentControl Wraps the body object with a Rich Text content control.
insertFileFromBase64(base64File: string, insertLocation: InsertLocation) Range Inserts a document into the body at the specified location. The insertLocation value can be 'Replace', 'Start' or 'End'.
insertHtml(html: string, insertLocation: InsertLocation) Range Inserts HTML at the specified location. The insertLocation value can be 'Replace', 'Start' or 'End'.
insertOoxml(ooxml: string, insertLocation: InsertLocation) Range Inserts OOXML at the specified location. The insertLocation value can be 'Replace', 'Start' or 'End'.
insertParagraph(paragraphText: string, insertLocation: InsertLocation) Paragraph Inserts a paragraph at the specified location. The insertLocation value can be 'Start' or 'End'.
insertText(text: string, insertLocation: InsertLocation) Range Inserts text into the body at the specified location. The insertLocation value can be 'Replace', 'Start' or 'End'.
load(param: object) void Fills the proxy object created in JavaScript layer with property and object values specified in the parameter.
search(searchText: string, searchOptions: ParamTypeStrings.SearchOptions) SearchResultCollection Performs a search with the specified searchOptions on the scope of the body object. The search results are a collection of range objects.
select() void Selects the body and navigates the Word UI to it.

Method Details

method()

Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.

Syntax

bodyObject.method();

Parameters

None

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to clear the contents of the body.
body.clear();

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Cleared the body contents.');

});

})
.catch(function (error) {
console.log("Error: "

+ JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log("Debug info: "

+ JSON.stringify(error.debugInfo));

}
});

clear()

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

Syntax

bodyObject.clear();

Parameters

None

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to clear the contents of the body.
body.clear();

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Cleared the body 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 the HTML representation of the body object.

Syntax

bodyObject.getHtml();

Parameters

None

Returns

string

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to get the HTML contents of the body.
var bodyHTML = body.getHtml();

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log("Body HTML contents: "

+ bodyHTML.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 OOXML (Office Open XML) representation of the body object.

Syntax

bodyObject.getOoxml();

Parameters

None

Returns

string

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to get the OOXML contents of the body.
var bodyOOXML = body.getOoxml();

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log("Body OOXML contents: "

+ bodyOOXML.value);

});

})
.catch(function (error) {
console.log("Error: "

+ JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log("Debug info: "

+ JSON.stringify(error.debugInfo));

}
});

insertBreak(breakType: BreakType, insertLocation: InsertLocation)

Inserts a break at the specified location. The insertLocation value can be 'Start' or 'End'.

Syntax

bodyObject.insertBreak(breakType, insertLocation);

Parameters

Parameter Type Description
breakType BreakType Required. The break type to add to the body.
insertLocation InsertLocation Required. The value can be 'Start' or 'End'.

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (ctx) {
// Create a proxy object for the document body.
var body = ctx.document.body;

// Queue a commmand to insert a page break at the start of the document body.
body.insertBreak(Word.BreakType.page, Word.InsertLocation.start);

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return ctx.sync().then(function () {
console.log('Added a page break at the start of the document body.');

});

})
.catch(function (error) {
console.log("Error: "

+ JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log("Debug info: "

+ JSON.stringify(error.debugInfo));

}
});

insertContentControl()

Wraps the body object with a Rich Text content control.

Syntax

bodyObject.insertContentControl();

Parameters

None

Returns

ContentControl

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to wrap the body in a content control.
body.insertContentControl();

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Wrapped the body in a 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: string, insertLocation: InsertLocation)

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

Syntax

bodyObject.insertFileFromBase64(base64File, insertLocation);

Parameters

Parameter Type Description
base64File string Required. The base64 encoded file contents to be inserted.
insertLocation InsertLocation Required. The value can be 'Replace', 'Start' or 'End'.

Returns

Range

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to insert base64 encoded .docx at the beginning of the content body.
// You will need to implement getBase64() to pass in a string of a base64 encoded docx file.
body.insertFileFromBase64(getBase64(), Word.InsertLocation.start);

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Added base64 encoded text to the beginning of the document body.');

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

insertHtml(html: string, insertLocation: InsertLocation)

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

Syntax

bodyObject.insertHtml(html, insertLocation);

Parameters

Parameter Type Description
html string Required. The HTML to be inserted in the document.
insertLocation InsertLocation Required. The value can be 'Replace', 'Start' or 'End'.

Returns

Range

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to insert HTML in to the beginning of the body.
body.insertHtml('**This is text inserted with body.insertHtml()**', Word.InsertLocation.start);

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('HTML added to the beginning of the document body.');

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

insertOoxml(ooxml: string, insertLocation: InsertLocation)

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

Syntax

bodyObject.insertOoxml(ooxml, insertLocation);

Parameters

Parameter Type Description
ooxml string Required. The OOXML to be inserted.
insertLocation InsertLocation Required. The value can be 'Replace', 'Start' or 'End'.

Returns

Range

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to insert OOXML in to the beginning of the body.
body.insertOoxml(""

+
"Hello world (this"

+
 "

should be bold, red, size 14).", Word.InsertLocation.start);

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('OOXML added to the beginning of the document body.');

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

insertParagraph(paragraphText: string, insertLocation: InsertLocation)

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

Syntax

bodyObject.insertParagraph(paragraphText, insertLocation);

Parameters

Parameter Type Description
paragraphText string Required. The paragraph text to be inserted.
insertLocation InsertLocation Required. The value can be 'Start' or 'End'.

Returns

Paragraph

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to insert the paragraph at the end of the document body.
body.insertParagraph('Content of a new paragraph', Word.InsertLocation.end);

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Paragraph added at the end of the document body.');

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

insertText(text: string, insertLocation: InsertLocation)

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

Syntax

bodyObject.insertText(text, insertLocation);

Parameters

Parameter Type Description
text string Required. Text to be inserted.
insertLocation InsertLocation Required. The value can be 'Replace', 'Start' or 'End'.

Returns

Range

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to insert text in to the beginning of the body.
body.insertText('This is text inserted with body.insertText()', Word.InsertLocation.start);

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Text added to the beginning of the document body.');

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

load(param: object)

Fills the proxy object created in JavaScript layer with property and object values specified in the parameter.

Syntax

object.load(param);

Parameters

Parameter Type Description
param object Optional. Accepts parameter and relationship names as delimited string or an array. Or, provide loadOption object.

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to load font and style information for the document body.
context.load(body, 'font/size, font/name, font/color, style');

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
        // Show the results of the load method. Here we show the
// property values on the body object.
var results = 'Font size: ' + body.font.size +
';

Font name: ' + body.font.name +
';

Font color: ' + body.font.color +
';

Body style: ' + body.style;

console.log(results);

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

search(searchText: string, searchOptions: ParamTypeStrings.SearchOptions)

Performs a search with the specified searchOptions on the scope of the body object. The search results are a collection of range objects.

Syntax

bodyObject.search(searchText, searchOptions);

Parameters

Parameter Type Description
searchText string Required. The search text.
searchOptions ParamTypeStrings.SearchOptions Optional. Optional. Options for the search.

Returns

SearchResultCollection

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Setup the search options.
var options = Word.SearchOptions.newObject(context);

options.matchCase = false
// Queue a commmand to search the document.
var searchResults = context.document.body.search('video', options);

// Queue a commmand to load the results.
context.load(searchResults, 'text, font');

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
var results = 'Found count: ' + searchResults.items.length + 
                      ';

we highlighted the results.';

// Queue a command to change the font for each found item. 
        for (var i = 0;

i <

searchResults.items.length;

i++) {
searchResults.items[i].font.color = '#FF0000'   

// Change color to Red
searchResults.items[i].font.highlightColor = '#FFFF00';

searchResults.items[i].font.bold = true;

}
// Synchronize the document state by executing the queued-up commands, 
        // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log(results);

});

    });

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

select()

Selects the body and navigates the Word UI to it.

Syntax

bodyObject.select();

Parameters

None

Returns

void

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to select the document body. The Word UI will 
    // move to the selected document body.
body.select();

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Selected the document body.');

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}
});

Property access examples

Get the text property on the body object

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to load the text in document body.
context.load(body, 'text');

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
console.log("Body contents: "

+ body.text);

});

})
.catch(function (error) {
console.log("Error: "

+ JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log("Debug info: "

+ JSON.stringify(error.debugInfo));

}
});

Get the style and the font size, font name, and font color properties on the body object.

// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;

// Queue a commmand to load font and style information for the document body.
context.load(body, 'font/size, font/name, font/color, style');

// Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion.
return context.sync().then(function () {
// Show the results of the load method. Here we show the
// property values on the body object.
var results = 'Font size: ' + body.font.size +
                      ';

Font name: ' + body.font.name +
';

Font color: ' + body.font.color +
';

Body style: ' + body.style;

console.log(results);

});

})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}

});