Skip to main content

Word API

Use Office.word(iframe) for an iframe that opened a Word document. The app handle exposes the document root and delegates its top-level methods, so word.getBody() is the same as word.getDocument().getBody().

First complete the distribution, import map, and HTTP server setup in the Editor SDK quickstart. Open this document type in the office-frame iframe. Use the JavaScript blocks below in order in the same module script. Write examples change the document, so run them on a sample or copy.

Object model

The root handle is Word.WordDocument. Common object types in the current catalog: Body, Section, Paragraph, Range, Table, TableRow, TableCell, Shape, Image, Bookmark, Hyperlink, List, and DocumentInfo.

Every handle method is a round trip to the editor and returns a Promise. Methods that return another object return a new handle.

Read and write text

import { Office, Word } from "@thinkfree.dev/tfo-sdk";

const iframe = document.querySelector("#office-frame");
const word = Office.word(iframe);
await word.whenReady();

const doc = word.getDocument();
const body = await doc.getBody();
console.log(await body.getText());

const title = await body.insertParagraph("Quarterly summary");
await title.setStyle(Word.Style.HEADING_1);

const para = await body.insertParagraph("Revenue grew 15% over the previous quarter.");
await para.setParagraphFormat({ align: Word.TextAlign.JUSTIFY, lineSpacing: 1.5 });

await doc.save();

Constants live in the Word namespace (for example Word.Style, Word.TextAlign, Word.ListType). Import the namespace together with Office.

Search and format ranges

const hits = await body.search("review");
for (const range of hits) {
await range.setTextFormat({ highlightColor: "#FFFF00", bold: true });
}

Use ranges and paragraph handles for targeted edits. Read the generated catalog before relying on a method because the preview surface is versioned with the editor artifact.

Tables and lists

const table = await body.insertTable(3, 2, {
values: [["Item", "Value"], ["Revenue", "120"], ["Cost", "80"]],
});

const first = await body.insertParagraph("Lock the plan");
const last = await body.insertParagraph("Start development");
const range = await doc.getRange({ startParaId: await first.getId(), endParaId: await last.getId() });
await range.setList(Word.ListType.NUMBER_DECIMAL); // apply once over the whole range to keep one numbering sequence

Errors

A failed document operation rejects with SDKError. The editor's structured response is available in error.data:

try {
await body.insertParagraph("…");
} catch (error) {
if (error.data?.error) console.warn(error.data.error.code, error.data.error.message);
}

See Document tools and error handling for the full error table.

API reference

Every object type available on Word handles in the current SDK build has its own reference page, generated from the SDK type definitions: a method summary table, then each method's TypeScript signature, parameters, and return value. Every method returns a Promise. Pick an object type below or open it from the sidebar.

Generated from the SDK type definitions

Word · 14 object types · 259 methods · namespace Word. This reference is generated from the API document artifact (sdk-api-doc.html) produced with the SDK build. Descriptions are the editor's API comments as published. Pin the SDK and Office versions together and regenerate when either changes.

Object typeMethodsObtained from
Word.Document37app.getDocument()
Word.Body24Document.getBody(), Section.getBody(), Section.getFooter(), …
Word.Section15Document.getSection(), Document.getSections(), Range.getParentSection()
Word.Paragraph44Document.getParagraph(), Body.getParagraph(), Body.getParagraphs(), …
Word.Range50Document.getRange(), Document.getSelection(), Body.getRange(), …
Word.Table22Document.getTable(), Body.insertTable(), Paragraph.getParentTable(), …
Word.TableRow6Table.getRow(), Table.insertRow()
Word.TableCell10Paragraph.getParentTableCell(), Table.getCell(), TableRow.getCell(), …
Word.Shape18Document.getShape(), Document.getShapes(), Paragraph.insertShape(), …
Word.Image18Paragraph.insertImage(), Paragraph.insertWebVideo(), Range.insertImage(), …
Word.Bookmark5Document.getBookmark(), Document.getBookmarks(), Range.getBookmarks(), …
Word.Hyperlink8Document.getHyperlink(), Document.getHyperlinks(), Range.getHyperlink(), …
Word.List2Paragraph.getList()
Word.DocumentInfo0-

Constant members are listed on the Word.* page.