Word.Section class

Represents a section in a Word document.

[ API set: WordApi 1.1 ]

Extends

Properties

body

Gets the body object of the section. This does not include the header/footer and other section metadata. 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.

Methods

getFooter(type)

Gets one of the section's footers.

[ API set: WordApi 1.1 ]

getFooter(typeString)

Gets one of the section's footers.

[ API set: WordApi 1.1 ]

getHeader(type)

Gets one of the section's headers.

[ API set: WordApi 1.1 ]

getHeader(typeString)

Gets one of the section's headers.

[ API set: WordApi 1.1 ]

getNext()

Gets the next section. Throws if this section is the last one.

[ API set: WordApi 1.3 ]

getNextOrNullObject()

Gets the next section. Returns a null object if this section is the last one.

[ API set: WordApi 1.3 ]

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.

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.

toJSON()
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.

Property Details

body

Gets the body object of the section. This does not include the header/footer and other section metadata. Read-only.

[ API set: WordApi 1.1 ]

readonly body: Word.Body;

Property Value

Word.Body

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

RequestContext

Method Details

getFooter(type)

Gets one of the section's footers.

[ API set: WordApi 1.1 ]

getFooter(type: Word.HeaderFooterType): Word.Body;

Parameters

type
Word.HeaderFooterType

Required. The type of footer to return. This value can be: 'Primary', 'FirstPage', or 'EvenPages'.

Returns

Word.Body

Examples

await Word.run(async (context) => {
  context.document.sections.getFirst().getFooter("Primary")
    .insertParagraph("This is a footer", "End");

  await context.sync();
});

getFooter(typeString)

Gets one of the section's footers.

[ API set: WordApi 1.1 ]

getFooter(typeString: "Primary" | "FirstPage" | "EvenPages"): Word.Body;

Parameters

typeString
"Primary" | "FirstPage" | "EvenPages"

Required. The type of footer to return. This value can be: 'Primary', 'FirstPage', or 'EvenPages'.

Returns

Word.Body

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {

    // Create a proxy sectionsCollection object.
    var mySections = context.document.sections;

    // Queue a commmand to load the sections.
    context.load(mySections, 'body/style');

    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {

        // Create a proxy object the primary footer of the first section. 
        // Note that the footer is a body object.
        var myFooter = mySections.items[0].getFooter("primary");

        // Queue a command to insert text at the end of the footer.
        myFooter.insertText("This is a footer.", Word.InsertLocation.end);

        // Queue a command to wrap the header in a content control.
        myFooter.insertContentControl();

        // Synchronize the document state by executing the queued commands, 
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log("Added a footer to the first section.");
        });                    
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

getHeader(type)

Gets one of the section's headers.

[ API set: WordApi 1.1 ]

getHeader(type: Word.HeaderFooterType): Word.Body;

Parameters

type
Word.HeaderFooterType

Required. The type of header to return. This value can be: 'Primary', 'FirstPage', or 'EvenPages'.

Returns

Word.Body

Examples

await Word.run(async (context) => {
  context.document.sections.getFirst().getHeader("Primary")
    .insertParagraph("This is a header", "End");

  await context.sync();
});

getHeader(typeString)

Gets one of the section's headers.

[ API set: WordApi 1.1 ]

getHeader(typeString: "Primary" | "FirstPage" | "EvenPages"): Word.Body;

Parameters

typeString
"Primary" | "FirstPage" | "EvenPages"

Required. The type of header to return. This value can be: 'Primary', 'FirstPage', or 'EvenPages'.

Returns

Word.Body

Examples

// Run a batch operation against the Word object model.
Word.run(function (context) {

    // Create a proxy sectionsCollection object.
    var mySections = context.document.sections;

    // Queue a commmand to load the sections.
    context.load(mySections, 'body/style');

    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {

        // Create a proxy object the primary header of the first section. 
        // Note that the header is a body object.
        var myHeader = mySections.items[0].getHeader("primary");

        // Queue a command to insert text at the end of the header.
        myHeader.insertText("This is a header.", Word.InsertLocation.end);

        // Queue a command to wrap the header in a content control.
        myHeader.insertContentControl();

        // Synchronize the document state by executing the queued commands, 
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log("Added a header to the first section.");
        });                    
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

getNext()

Gets the next section. Throws if this section is the last one.

[ API set: WordApi 1.3 ]

getNext(): Word.Section;

Returns

Word.Section

getNextOrNullObject()

Gets the next section. Returns a null object if this section is the last one.

[ API set: WordApi 1.3 ]

getNextOrNullObject(): Word.Section;

Returns

Word.Section

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.SectionLoadOptions): Word.Section;

Parameters

option
Word.Interfaces.SectionLoadOptions

Returns

Word.Section

Remarks

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

load(option?: string | string[]): Word.Section - 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.Section - 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.Section - 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.

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.Section;

Parameters

propertyNames
string | string[]

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

Returns

Word.Section

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.Section;

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

Word.Section

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.SectionUpdateData, options?: OfficeExtension.UpdateOptions): void;

Parameters

properties
Interfaces.SectionUpdateData

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.Section): void

set(properties)

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

set(properties: Word.Section): void;

Parameters

properties
Word.Section

Returns

void

toJSON()

toJSON(): Word.Interfaces.SectionData;

Returns

Word.Interfaces.SectionData

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.Section;

Returns

Word.Section

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.Section;

Returns

Word.Section