Word.Document.styles is undefined at runtime in Word for Mac 16.87 (WordApi 1.4 manifest)

步清 苏 0 Reputation points
2025-05-18T16:12:13.88+00:00

Body:

We are developing a Word Office Add-in and encountering a persistent issue where context.document.styles evaluates to undefined at runtime. This occurs specifically in Microsoft Word for Mac version 16.87, even when our add-in's manifest specifies <Set Name="WordApi" MinVersion="1.4"/>.

We understand that MinVersion="1.4" implies compatibility with earlier API sets, including WordApi 1.1 where document.styles was introduced. Standard Office.js practice dictates using context.document.load('styles'); await context.sync(); before accessing the styles property. We have rigorously applied this pattern, yet the styles property remains undefined after the sync() call. This prevents access to the StyleCollection and related methods like styles.getByName() (WordApi 1.3).

This issue pertains to the Office.js API (Word JavaScript API) within an Office Add-in context, specifically on macOS.

Environment:

  • Office Application: Microsoft Word for Mac

Word Version: 16.87 Operating System: macOS Office.js Source: CDN (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-mac-16.00.js - observed from console logs) @types/office-js version: 1.0.501 (though the primary issue is runtime behavior) Manifest Requirement Set for WordApi: <Set Name="WordApi" MinVersion="1.4"/>

Expected Behavior: context.document.styles should be a valid Word.StyleCollection object after context.document.load('styles'); await context.sync(); is successfully executed, allowing subsequent calls like styles.getByName().

Actual Behavior: Despite correctly using context.document.load('styles'); await context.sync();, the context.document.styles property is undefined at runtime. Consequently, any attempt to use it fails. This was confirmed using a minimal test case (see below).

What We've Ruled Out:

Incorrect load/sync pattern: Our minimal test case explicitly uses the correct load('styles') and sync() sequence. Misunderstanding of MinVersion: We understand MinVersion="1.4" should support WordApi 1.1 features. The issue is not simply a TypeScript type definition mismatch, as runtime checks confirm the property is undefined.

Minimal Reproducible Example (TypeScript/JavaScript):

TypeScript

async

Console Log Output from Minimal Test:

>>> testStylesAvailability CALLED
Simple Test: Inside Word.run.
Simple Test: context.document object is available.
Simple Test: Attempting context.document.load('styles')
Simple Test: After context.document.load('styles') and sync.
Simple Test: Type of context.document.styles is now: - "undefined"
Simple Test: Value of context.document.styles is now: - undefined
Simple Test: FAILURE - context.document.styles is undefined, null, or does not have getByName function.
Simple Test: Word.run execution finished.

Impact: This issue prevents programmatic manipulation of style definitions through the standard document.styles collection in this specific environment, significantly hindering add-in functionality related to document templating and formatting.

We also briefly attempted context.document.getStyleByName() (WordApi 1.5) which resulted in a TypeError: context.document.getStyleByName is not a function, likely due to the environment strictly adhering to the manifest's 1.4 requirement or the same underlying issue affecting document.styles. Our primary concern is the unavailability of the WordApi 1.1 document.styles property.

Any insights, workarounds, or confirmation of this being a known issue would be greatly appreciated.


中文翻译对照(供您参考,实际提交时请使用上面的英文版):

标题: Word for Mac 16.87 (WordApi 1.4 manifest)环境下,Word.Document.styles 在运行时为 undefined

正文:

我们正在开发一个 Word Office 加载项,遇到了一个持续存在的问题:在 Microsoft Word for Mac 16.87 版本中,即使我们的加载项 manifest 文件指定了 <Set Name="WordApi" MinVersion="1.4"/>context.document.styles 在运行时仍然解析为 undefined

我们理解 MinVersion="1.4" 意味着对早期 API 集(包括引入了 document.styles 的 WordApi 1.1)的兼容。标准的 Office.js 实践要求在访问 styles 属性前使用 context.document.load('styles'); await context.sync();我们已严格应用此模式,但在 sync() 调用后,styles 属性Body:

We are developing a Word Office Add-in and encountering a persistent issue where context.document.styles evaluates to undefined at runtime. This occurs specifically in Microsoft Word for Mac version 16.87, even when our add-in's manifest specifies <Set Name="WordApi" MinVersion="1.4"/>.

We understand that MinVersion="1.4" implies compatibility with earlier API sets, including WordApi 1.1 where document.styles was introduced. Standard Office.js practice dictates using context.document.load('styles'); await context.sync(); before accessing the styles property. We have rigorously applied this pattern, yet the styles property remains undefined after the sync() call. This prevents access to the StyleCollection and related methods like styles.getByName() (WordApi 1.3).

This issue pertains to the Office.js API (Word JavaScript API) within an Office Add-in context, specifically on macOS.

Environment:

Office Application: Microsoft Word for Mac

Word Version: 16.87

Operating System: macOS

Office.js Source: CDN (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-mac-16.00.js - observed from console logs)

@types/office-js version: 1.0.501 (though the primary issue is runtime behavior)

Manifest Requirement Set for WordApi: <Set Name="WordApi" MinVersion="1.4"/>

Expected Behavior: context.document.styles should be a valid Word.StyleCollection object after context.document.load('styles'); await context.sync(); is successfully executed, allowing subsequent calls like styles.getByName().

Actual Behavior: Despite correctly using context.document.load('styles'); await context.sync();, the context.document.styles property is undefined at runtime. Consequently, any attempt to use it fails. This was confirmed using a minimal test case (see below).

What We've Ruled Out:

Incorrect load/sync pattern: Our minimal test case explicitly uses the correct load('styles') and sync() sequence.

Misunderstanding of MinVersion: We understand MinVersion="1.4" should support WordApi 1.1 features.

The issue is not simply a TypeScript type definition mismatch, as runtime checks confirm the property is undefined.

Minimal Reproducible Example (TypeScript/JavaScript):

TypeScript

async

Console Log Output from Minimal Test:

>>> testStylesAvailability CALLED
Simple Test: Inside Word.run.
Simple Test: context.document object is available.
Simple Test: Attempting context.document.load('styles')
Simple Test: After context.document.load('styles') and sync.
Simple Test: Type of context.document.styles is now: - "undefined"
Simple Test: Value of context.document.styles is now: - undefined
Simple Test: FAILURE - context.document.styles is undefined, null, or does not have getByName function.
Simple Test: Word.run execution finished.

Impact: This issue prevents programmatic manipulation of style definitions through the standard document.styles collection in this specific environment, significantly hindering add-in functionality related to document templating and formatting.

We also briefly attempted context.document.getStyleByName() (WordApi 1.5) which resulted in a TypeError: context.document.getStyleByName is not a function, likely due to the environment strictly adhering to the manifest's 1.4 requirement or the same underlying issue affecting document.styles. Our primary concern is the unavailability of the WordApi 1.1 document.styles property.

Any insights, workarounds, or confirmation of this being a known issue would be greatly appreciated.


JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,062 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.