|
| 1 | +--- |
| 2 | +title: insertAnnotations method (Word JavaScript API) |
| 3 | +description: Adds ephemeral critique annotations (such as grammar underlines) to a paragraph in Word using Office.js. |
| 4 | +ms.date: 04/22/2025 |
| 5 | +ms.topic: reference |
| 6 | +--- |
| 7 | + |
| 8 | +# insertAnnotations (Word JavaScript API) |
| 9 | + |
| 10 | +> [!NOTE] |
| 11 | +> This API is part of the `AnnotationSet` feature in Word JavaScript API and is designed for applying non-persistent, UI-focused annotations. This page provides developer-focused usage, behavior notes, and real-world examples for use in Word add-ins. |
| 12 | +
|
| 13 | +## Summary |
| 14 | + |
| 15 | +The `insertAnnotations` method allows developers to programmatically apply ephemeral critique annotations to a Word paragraph. These annotations appear as grammar-style underlines with contextual tooltips and are: |
| 16 | + |
| 17 | +- **Non-destructive**: They don’t modify the document content. |
| 18 | +- **Ephemeral**: They disappear once the document is closed or reloaded. |
| 19 | +- **UI-oriented**: Ideal for grammar checkers, intelligent suggestions, and contextual UI. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```javascript |
| 24 | +paragraph.insertAnnotations({ |
| 25 | + critiques: [ |
| 26 | + { |
| 27 | + start: number, |
| 28 | + length: number, |
| 29 | + colorScheme: Word.CritiqueColorScheme.red, |
| 30 | + popupOptions: { |
| 31 | + titleResourceId: string, |
| 32 | + subtitleResourceId: string, |
| 33 | + brandingTextResourceId: string, |
| 34 | + suggestions: string[] |
| 35 | + } |
| 36 | + } |
| 37 | + ] |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +## Parameters |
| 42 | + |
| 43 | +| Property | Type | Description | |
| 44 | +|-----------------|----------------------------------|-------------| |
| 45 | +| `start` | `number` | Start index (character offset) within the paragraph text where the critique should begin. | |
| 46 | +| `length` | `number` | Number of characters the critique annotation should cover. | |
| 47 | +| `colorScheme` | `Word.CritiqueColorScheme` | Specifies the color of the underline (e.g., `red`, `green`, `blue`). | |
| 48 | +| `popupOptions` | `object` | Metadata for the annotation’s tooltip. See below for its properties. | |
| 49 | + |
| 50 | +### `popupOptions` properties |
| 51 | + |
| 52 | +| Property | Type | Description | |
| 53 | +|----------------------------|----------|-------------| |
| 54 | +| `titleResourceId` | `string` | Resource ID for the tooltip title. | |
| 55 | +| `subtitleResourceId` | `string` | Resource ID for the tooltip subtitle. | |
| 56 | +| `brandingTextResourceId` | `string` | Resource ID for the tooltip's branding or footer text. | |
| 57 | +| `suggestions` | `string[]` | A list of suggested alternative texts shown in the tooltip. | |
| 58 | + |
| 59 | +## Returns |
| 60 | + |
| 61 | +Returns a `ClientResult<string[]>` object containing the list of unique annotation IDs created. |
| 62 | + |
| 63 | +You must call `await context.sync()` before accessing the `.value` property to retrieve the IDs: |
| 64 | + |
| 65 | +```javascript |
| 66 | +const annotationIds = paragraph.insertAnnotations(annotationSet); |
| 67 | +await context.sync(); |
| 68 | +console.log("Inserted annotation IDs:", annotationIds.value); // Example: ["id1", "id2"] |
| 69 | +``` |
| 70 | + |
| 71 | +These IDs can be used with other annotation APIs, such as `getAnnotationById()`. |
| 72 | + |
| 73 | +## Example |
| 74 | + |
| 75 | +```javascript |
| 76 | +await Word.run(async (context) => { |
| 77 | + const paragraph = context.document.getSelection().paragraphs.getFirst(); |
| 78 | + |
| 79 | + const popupOptions = { |
| 80 | + brandingTextResourceId: "Demo.Branding", |
| 81 | + titleResourceId: "Demo.Title", |
| 82 | + subtitleResourceId: "Demo.Subtitle", |
| 83 | + suggestions: ["suggestion 1", "suggestion 2"] |
| 84 | + }; |
| 85 | + |
| 86 | + const annotationSet = { |
| 87 | + critiques: [ |
| 88 | + { |
| 89 | + start: 0, |
| 90 | + length: 4, |
| 91 | + colorScheme: Word.CritiqueColorScheme.red, |
| 92 | + popupOptions |
| 93 | + } |
| 94 | + ] |
| 95 | + }; |
| 96 | + |
| 97 | + const annotationIds = paragraph.insertAnnotations(annotationSet); |
| 98 | + await context.sync(); |
| 99 | + |
| 100 | + console.log("Inserted annotation IDs:", annotationIds.value); |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +## Notes |
| 105 | + |
| 106 | +- Annotations are **in-memory only** and do **not persist** in the `.docx` file. |
| 107 | +- They are **removed** when the document is closed or reloaded. |
| 108 | +- Useful for **real-time UI enhancements** (e.g., spelling/grammar checkers, writing suggestions). |
| 109 | +- Use returned IDs to manage critiques (e.g., retrieve or delete specific annotations). |
| 110 | + |
| 111 | +## Related Links |
| 112 | + |
| 113 | +- [AnnotationSet](https://learn.microsoft.com/javascript/api/word/word.annotationset) |
| 114 | +- [getAnnotationById()](https://learn.microsoft.com/javascript/api/word/word.document?view=word-js-preview#word-document-getannotationbyidid) |
| 115 | +- [setAnnotation()](https://learn.microsoft.com/javascript/api/word/word.range?view=word-js-preview#setannotationoptions-) |
0 commit comments