Edit

Share via


Word.Window class

Represents the window that displays the document. A window can be split to contain multiple reading panes.

Extends

Remarks

[ API set: WordApiDesktop 1.2 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/35-ranges/get-pages.yaml

await Word.run(async (context) => {
  // Gets the first paragraph of each page.
  console.log("Getting first paragraph of each page...");

  // Get the active window.
  const activeWindow: Word.Window = context.document.activeWindow;
  activeWindow.load();

  // Get the active pane.
  const activePane: Word.Pane = activeWindow.activePane;
  activePane.load();

  // Get all pages.
  const pages: Word.PageCollection = activePane.pages;
  pages.load();

  await context.sync();

  // Get page index and paragraphs of each page.
  const pagesIndexes = [];
  const pagesNumberOfParagraphs = [];
  const pagesFirstParagraphText = [];
  for (let i = 0; i < pages.items.length; i++) {
    const page = pages.items[i];
    page.load("index");
    pagesIndexes.push(page);

    const paragraphs = page.getRange().paragraphs;
    paragraphs.load("items/length");
    pagesNumberOfParagraphs.push(paragraphs);

    const firstParagraph = paragraphs.getFirst();
    firstParagraph.load("text");
    pagesFirstParagraphText.push(firstParagraph);
  }

  await context.sync();

  for (let i = 0; i < pagesIndexes.length; i++) {
    console.log(`Page index: ${pagesIndexes[i].index}`);
    console.log(`Number of paragraphs: ${pagesNumberOfParagraphs[i].items.length}`);
    console.log("First paragraph's text:", pagesFirstParagraphText[i].text);
  }
});

Properties

activePane

Gets the active pane in the window.

areRulersDisplayed

Specifies whether rulers are displayed for the window or pane.

areScreenTipsDisplayed

Specifies whether comments, footnotes, endnotes, and hyperlinks are displayed as tips.

areThumbnailsDisplayed

Specifies whether thumbnail images of the pages in a document are displayed along the left side of the Microsoft Word document window.

caption

Specifies the caption text for the window that is displayed in the title bar of the document or application window.

context

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

height

Specifies the height of the window (in points).

horizontalPercentScrolled

Specifies the horizontal scroll position as a percentage of the document width.

imeMode

Specifies the default start-up mode for the Japanese Input Method Editor (IME).

index

Gets the position of an item in a collection.

isActive

Specifies whether the window is active.

isDocumentMapVisible

Specifies whether the document map is visible.

isEnvelopeVisible

Specifies whether the email message header is visible in the document window. The default value is false.

isHorizontalScrollBarDisplayed

Specifies whether a horizontal scroll bar is displayed for the window.

isLeftScrollBarDisplayed

Specifies whether the vertical scroll bar appears on the left side of the document window.

isRightRulerDisplayed

Specifies whether the vertical ruler appears on the right side of the document window in print layout view.

isSplit

Specifies whether the window is split into multiple panes.

isVerticalRulerDisplayed

Specifies whether a vertical ruler is displayed for the window or pane.

isVerticalScrollBarDisplayed

Specifies whether a vertical scroll bar is displayed for the window.

isVisible

Specifies whether the window is visible.

left

Specifies the horizontal position of the window, measured in points.

next

Gets the next document window in the collection of open document windows.

panes

Gets the collection of panes in the window.

previous

Gets the previous document window in the collection open document windows.

showSourceDocuments

Specifies how Microsoft Word displays source documents after a compare and merge process.

splitVertical

Specifies the vertical split percentage for the window.

state

Specifies the state of the document window or task window.

styleAreaWidth

Specifies the width of the style area in points.

top

Specifies the vertical position of the document window, in points.

type

Gets the window type.

usableHeight

Gets the height (in points) of the active working area in the document window.

usableWidth

Gets the width (in points) of the active working area in the document window.

verticalPercentScrolled

Specifies the vertical scroll position as a percentage of the document length.

view

Gets the View object that represents the view for the window.

width

Specifies the width of the document window, in points.

windowNumber

Gets an integer that represents the position of the window.

Methods

activate()

Activates the window.

close(options)

Closes the window.

largeScroll(options)

Scrolls the window by the specified number of screens.

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.

pageScroll(options)

Scrolls through the window page by page.

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.

setFocus()

Sets the focus of the document window to the body of an email message.

smallScroll(options)

Scrolls the window by the specified number of lines. A "line" corresponds to the distance scrolled by clicking the scroll arrow on the scroll bar once.

toggleRibbon()

Shows or hides the ribbon.

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's passed to it.) Whereas the original Word.Window object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.WindowData) 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

activePane

Gets the active pane in the window.

readonly activePane: Word.Pane;

Property Value

Remarks

[ API set: WordApiDesktop 1.2 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/35-ranges/get-pages.yaml

await Word.run(async (context) => {
  // Gets the pages enclosing the viewport.

  // Get the active window.
  const activeWindow: Word.Window = context.document.activeWindow;
  activeWindow.load();

  // Get the active pane.
  const activePane: Word.Pane = activeWindow.activePane;
  activePane.load();

  // Get pages enclosing the viewport.
  const pages: Word.PageCollection = activePane.pagesEnclosingViewport;
  pages.load();

  await context.sync();

  // Log the number of pages.
  const pageCount = pages.items.length;
  console.log(`Number of pages enclosing the viewport: ${pageCount}`);

  // Log index info of these pages.
  const pagesIndexes = [];
  for (let i = 0; i < pageCount; i++) {
    const page = pages.items[i];
    page.load("index");
    pagesIndexes.push(page);
  }

  await context.sync();

  for (let i = 0; i < pagesIndexes.length; i++) {
    console.log(`Page index: ${pagesIndexes[i].index}`);
  }
});

areRulersDisplayed

Specifies whether rulers are displayed for the window or pane.

areRulersDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

areScreenTipsDisplayed

Specifies whether comments, footnotes, endnotes, and hyperlinks are displayed as tips.

readonly areScreenTipsDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

areThumbnailsDisplayed

Specifies whether thumbnail images of the pages in a document are displayed along the left side of the Microsoft Word document window.

areThumbnailsDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

caption

Specifies the caption text for the window that is displayed in the title bar of the document or application window.

caption: string;

Property Value

string

Remarks

[ API set: WordApiDesktop 1.4 ]

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

height

Specifies the height of the window (in points).

readonly height: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

horizontalPercentScrolled

Specifies the horizontal scroll position as a percentage of the document width.

horizontalPercentScrolled: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

imeMode

Specifies the default start-up mode for the Japanese Input Method Editor (IME).

imeMode: Word.ImeMode | "NoControl" | "On" | "Off" | "Hiragana" | "Katakana" | "KatakanaHalf" | "AlphaFull" | "Alpha" | "HangulFull" | "Hangul";

Property Value

Word.ImeMode | "NoControl" | "On" | "Off" | "Hiragana" | "Katakana" | "KatakanaHalf" | "AlphaFull" | "Alpha" | "HangulFull" | "Hangul"

Remarks

[ API set: WordApiDesktop 1.4 ]

index

Gets the position of an item in a collection.

readonly index: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

isActive

Specifies whether the window is active.

readonly isActive: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isDocumentMapVisible

Specifies whether the document map is visible.

isDocumentMapVisible: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isEnvelopeVisible

Specifies whether the email message header is visible in the document window. The default value is false.

isEnvelopeVisible: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isHorizontalScrollBarDisplayed

Specifies whether a horizontal scroll bar is displayed for the window.

isHorizontalScrollBarDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isLeftScrollBarDisplayed

Specifies whether the vertical scroll bar appears on the left side of the document window.

isLeftScrollBarDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isRightRulerDisplayed

Specifies whether the vertical ruler appears on the right side of the document window in print layout view.

isRightRulerDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isSplit

Specifies whether the window is split into multiple panes.

isSplit: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isVerticalRulerDisplayed

Specifies whether a vertical ruler is displayed for the window or pane.

isVerticalRulerDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isVerticalScrollBarDisplayed

Specifies whether a vertical scroll bar is displayed for the window.

isVerticalScrollBarDisplayed: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

isVisible

Specifies whether the window is visible.

isVisible: boolean;

Property Value

boolean

Remarks

[ API set: WordApiDesktop 1.4 ]

left

Specifies the horizontal position of the window, measured in points.

readonly left: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

next

Gets the next document window in the collection of open document windows.

readonly next: Word.Window;

Property Value

Remarks

[ API set: WordApiDesktop 1.4 ]

panes

Gets the collection of panes in the window.

readonly panes: Word.PaneCollection;

Property Value

Remarks

[ API set: WordApiDesktop 1.2 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/35-ranges/get-pages.yaml

await Word.run(async (context) => {
  // Gets all the panes in the active document window.

  // Get the active window.
  const activeWindow: Word.Window = context.document.activeWindow;
  activeWindow.load("panes/items/length");

  await context.sync();

  const panes: Word.PaneCollection = activeWindow.panes;
  console.log(`Number of panes in the current document window: ${panes.items.length}`);
});

previous

Gets the previous document window in the collection open document windows.

readonly previous: Word.Window;

Property Value

Remarks

[ API set: WordApiDesktop 1.4 ]

showSourceDocuments

Specifies how Microsoft Word displays source documents after a compare and merge process.

showSourceDocuments: Word.ShowSourceDocuments | "None" | "Original" | "Revised" | "Both";

Property Value

Word.ShowSourceDocuments | "None" | "Original" | "Revised" | "Both"

Remarks

[ API set: WordApiDesktop 1.4 ]

splitVertical

Specifies the vertical split percentage for the window.

splitVertical: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

state

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.

Specifies the state of the document window or task window.

state: Word.WindowState | "Normal" | "Maximize" | "Minimize";

Property Value

Word.WindowState | "Normal" | "Maximize" | "Minimize"

Remarks

[ API set: WordApi BETA (PREVIEW ONLY) ]

styleAreaWidth

Specifies the width of the style area in points.

styleAreaWidth: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

top

Specifies the vertical position of the document window, in points.

readonly top: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

type

Gets the window type.

readonly type: Word.WindowType | "Document" | "Template";

Property Value

Word.WindowType | "Document" | "Template"

Remarks

[ API set: WordApiDesktop 1.4 ]

usableHeight

Gets the height (in points) of the active working area in the document window.

readonly usableHeight: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

usableWidth

Gets the width (in points) of the active working area in the document window.

readonly usableWidth: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

verticalPercentScrolled

Specifies the vertical scroll position as a percentage of the document length.

verticalPercentScrolled: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

view

Gets the View object that represents the view for the window.

readonly view: Word.View;

Property Value

Remarks

[ API set: WordApiDesktop 1.4 ]

width

Specifies the width of the document window, in points.

readonly width: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

windowNumber

Gets an integer that represents the position of the window.

readonly windowNumber: number;

Property Value

number

Remarks

[ API set: WordApiDesktop 1.4 ]

Method Details

activate()

Activates the window.

activate(): void;

Returns

void

Remarks

[ API set: WordApiDesktop 1.4 ]

close(options)

Closes the window.

close(options?: Word.WindowCloseOptions): void;

Parameters

options
Word.WindowCloseOptions

Optional. The options that define whether to save changes before closing and whether to route the document.

Returns

void

Remarks

[ API set: WordApiDesktop 1.4 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/close-document-window.yaml

await Word.run(async (context) => {
  // Closes the document window, prompting to save if this is a new document.
  const window: Word.Window = context.document.activeWindow;
  const closeOptions: Word.WindowCloseOptions = { saveChanges: Word.SaveConfiguration.promptToSaveChanges };
  console.log("About to close the document window...");
  window.close(closeOptions);
});

largeScroll(options)

Scrolls the window by the specified number of screens.

largeScroll(options?: Word.WindowScrollOptions): void;

Parameters

options
Word.WindowScrollOptions

Optional. The options for scrolling the window by the specified number of screens. If no options are specified, the window is scrolled down one screen.

Returns

void

Remarks

[ API set: WordApiDesktop 1.4 ]

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.WindowLoadOptions): Word.Window;

Parameters

options
Word.Interfaces.WindowLoadOptions

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

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

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

pageScroll(options)

Scrolls through the window page by page.

pageScroll(options?: Word.WindowPageScrollOptions): void;

Parameters

options
Word.WindowPageScrollOptions

Optional. The options for scrolling through the window page by page.

Returns

void

Remarks

[ API set: WordApiDesktop 1.4 ]

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

Parameters

properties
Word.Interfaces.WindowUpdateData

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

Parameters

properties
Word.Window

Returns

void

setFocus()

Sets the focus of the document window to the body of an email message.

setFocus(): void;

Returns

void

Remarks

[ API set: WordApiDesktop 1.4 ]

smallScroll(options)

Scrolls the window by the specified number of lines. A "line" corresponds to the distance scrolled by clicking the scroll arrow on the scroll bar once.

smallScroll(options?: Word.WindowScrollOptions): void;

Parameters

options
Word.WindowScrollOptions

Optional. The options for scrolling the window by the specified number of lines. If no options are specified, the window is scrolled down by one line.

Returns

void

Remarks

[ API set: WordApiDesktop 1.4 ]

toggleRibbon()

Shows or hides the ribbon.

toggleRibbon(): void;

Returns

void

Remarks

[ API set: WordApiDesktop 1.4 ]

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's passed to it.) Whereas the original Word.Window object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.WindowData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Word.Interfaces.WindowData;

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

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

Returns