Word.Section class

Represents a section in a Word document.

Extends

Remarks

[ API set: WordApi 1.1 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/insert-section-breaks.yaml

// Inserts a section break on the next page.
await Word.run(async (context) => {
  const body = context.document.body;
  body.insertBreak(Word.BreakType.sectionNext, Word.InsertLocation.end);

  await context.sync();

  console.log("Inserted section break on next page");
});

Properties

body

Gets the body object of the section. This doesn't include the header/footer and other section metadata.

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.

getFooter(typeString)

Gets one of the section's footers.

getHeader(type)

Gets one of the section's headers.

getHeader(typeString)

Gets one of the section's headers.

getNext()

Gets the next section. Throws an ItemNotFound error if this section is the last one.

getNextOrNullObject()

Gets the next section. If this section is the last one, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

load(options)

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

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.Section object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.SectionData) 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're 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 need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.

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'll need to call context.sync() before the memory release takes effect.

Property Details

body

Gets the body object of the section. This doesn't include the header/footer and other section metadata.

readonly body: Word.Body;

Property Value

Remarks

[ 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.

context: RequestContext;

Property Value

Method Details

getFooter(type)

Gets one of the section's footers.

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

Parameters

type
Word.HeaderFooterType

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

Returns

Remarks

[ API set: WordApi 1.1 ]

Examples

// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Create a proxy sectionsCollection object.
    const mySections = context.document.sections;
    
    // Queue a command to load the sections.
    mySections.load('body/style');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
        
    // Create a proxy object the primary footer of the first section.
    // Note that the footer is a body object.
    const myFooter = mySections.items[0].getFooter(Word.HeaderFooterType.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.
    await context.sync();
    console.log("Added a footer to the first section.");   
});  

getFooter(typeString)

Gets one of the section's footers.

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

Parameters

typeString

"Primary" | "FirstPage" | "EvenPages"

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

Returns

Remarks

[ API set: WordApi 1.1 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/25-paragraph/insert-header-and-footer.yaml

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

  await context.sync();
});

getHeader(type)

Gets one of the section's headers.

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

Parameters

type
Word.HeaderFooterType

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

Returns

Remarks

[ API set: WordApi 1.1 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/25-paragraph/insert-header-and-footer.yaml

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

  await context.sync();
});

getHeader(typeString)

Gets one of the section's headers.

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

Parameters

typeString

"Primary" | "FirstPage" | "EvenPages"

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

Returns

Remarks

[ API set: WordApi 1.1 ]

Examples

// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Create a proxy sectionsCollection object.
    const mySections = context.document.sections;
    
    // Queue a command to load the sections.
    mySections.load('body/style');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
    
    // Create a proxy object the primary header of the first section.
    // Note that the header is a body object.
    const 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.
    await context.sync();
    console.log("Added a header to the first section.");
});  

getNext()

Gets the next section. Throws an ItemNotFound error if this section is the last one.

getNext(): Word.Section;

Returns

Remarks

[ API set: WordApi 1.3 ]

getNextOrNullObject()

Gets the next section. If this section is the last one, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getNextOrNullObject(): Word.Section;

Returns

Remarks

[ API set: WordApi 1.3 ]

load(options)

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

load(options?: Word.Interfaces.SectionLoadOptions): Word.Section;

Parameters

options
Word.Interfaces.SectionLoadOptions

Provides options for which properties of the object to load.

Returns

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

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; }

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

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
Word.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

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

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.Section object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.SectionData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Word.Interfaces.SectionData;

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're 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 need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.

track(): Word.Section;

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'll need to call context.sync() before the memory release takes effect.

untrack(): Word.Section;

Returns