How to Send Word Document Structure via API in Office Add-ins
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.

- 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.
Ensure you have the proper MSAL authentication configured for Microsoft Graph and that your Word Add-in project is successfully initialized with Office.js.
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.
Utilize the Office.js Word API to iterate through document objects such as paragraphs, tables, and sections within the active document.
Extract the properties of these traversed objects and serialize the combined document structure into a JSON payload to send with your text.
If your application explicitly requires the original DOCX file, authenticate with MSAL and call the Microsoft Graph API to download the file.
Retrieve the returned file bytes and encode them as Base64 on either the client or server side before sending them in your API request.

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. Download the installer: Visit the official WPS Office website and download the free installer for your operating system.
- 2. Install the software: Run the setup file and follow the on-screen instructions to complete the installation.
- 3. Open your documents: Launch WPS Writer and open your existing DOCX files to instantly view your complex document structures perfectly preserved.

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.




