logo
search
Others

How to Send Word Document Structure via API in Office Add-ins

Amos GikundaAmos Gikunda Sep 30, 2026 871 views

Question details

The user needs to retrieve and send the complete document structure, including paragraphs, tables, and sections, or the original DOCX file via an API request for AI Q&A retrieval.

How to Send Word Document Structure via API in Office Add-ins
Product
Microsoft Word
Device & OS
not provided
Scenario
Developing an Office Add-in or application that requires structured document content for AI processing and question-and-answer retrieval.
Observed behavior
Using the default body.getText() method only retrieves plain text, completely omitting the necessary document structure formatting like tables and sections.
Before you start

Ensure you have the proper MSAL authentication configured for Microsoft Graph and that your Word Add-in project is successfully initialized with Office.js.

Solution 1Recommended

Serialize Document Structure Using Office.js and Microsoft Graph

Extract document elements as JSON using the Word API, and download the full DOCX file via Microsoft Graph to convert it to Base64.

Since the basic text extraction method strips away formatting, you must manually traverse the document objects or fetch the entire file bytes to maintain the structure.

1
Traverse document objects

Utilize the Office.js Word API to iterate through document objects such as paragraphs, tables, and sections within the active document.

2
Serialize to JSON

Extract the properties of these traversed objects and serialize the combined document structure into a JSON payload to send with your text.

3
Download DOCX via Microsoft Graph

If your application explicitly requires the original DOCX file, authenticate with MSAL and call the Microsoft Graph API to download the file.

4
Encode and send bytes

Retrieve the returned file bytes and encode them as Base64 on either the client or server side before sending them in your API request.

Serialize Document Structure Using Office.js and Microsoft Graph
Python Implementation Note: If you are using Python to process the Graph API response, use response.content for the document bytes and avoid the obsolete Python 2 encode('base64') method.
Free Microsoft Office alternative

Experience Seamless Document Editing with WPS Office

While developing Microsoft Office add-ins can be highly complex, WPS Office offers a free, lightweight, and incredibly compatible suite for your everyday document creation and formatting needs.

  1. 1. Download the installer: Visit the official WPS Office website and download the free installer for your operating system.
  2. 2. Install the software: Run the setup file and follow the on-screen instructions to complete the installation.
  3. 3. Open your documents: Launch WPS Writer and open your existing DOCX files to instantly view your complex document structures perfectly preserved.
Fully compatible with Microsoft Word DOCX, DOC, and RTF formats.Lightweight architecture ensures fast launch times and smooth performance on all devices.Free to use with a familiar, easy-to-navigate user interface.Excellent support for rich document structures, complex tables, and sections.
microsoft office alternative - wps office

Frequently Asked Questions

Why does body.getText() omit document tables and sections?

The body.getText() method in Office.js is specifically designed to extract only the raw textual content, stripping away structural elements like paragraphs, tables, and sections by default.

Can I obtain the DOCX file seamlessly as Base64 directly from the Word add-in?

No, you cannot obtain the file directly as Base64 using just the Office.js API. You must use Microsoft Graph or another supported file-download method to retrieve the document bytes, and then encode them as Base64 on your client or server.

What is the recommended way to download the document structure in Python?

Authenticate using MSAL, use Microsoft Graph to fetch the file, and read the bytes using response.content. Do not use the obsolete Python 2 encode('base64') method.