Skip to content

Commit 0fa3b03

Browse files
[Word] (Window) Add snippet for close method (#1007)
1 parent 79ddaa2 commit 0fa3b03

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

playlists-prod/word.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,15 @@
580580
group: Scenarios
581581
api_set:
582582
WordApi: '1.4'
583+
- id: word-close-document-window
584+
name: Close document window
585+
fileName: close-document-window.yaml
586+
description: Shows how to close document window.
587+
rawUrl: >-
588+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/close-document-window.yaml
589+
group: Preview APIs
590+
api_set:
591+
WordApi: '1.10'
583592
- id: word-insert-and-change-content-controls
584593
name: Content control basics
585594
fileName: insert-and-change-content-controls.yaml

playlists/word.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,15 @@
580580
group: Scenarios
581581
api_set:
582582
WordApi: '1.4'
583+
- id: word-close-document-window
584+
name: Close document window
585+
fileName: close-document-window.yaml
586+
description: Shows how to close document window.
587+
rawUrl: >-
588+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/word/99-preview-apis/close-document-window.yaml
589+
group: Preview APIs
590+
api_set:
591+
WordApi: '1.10'
583592
- id: word-insert-and-change-content-controls
584593
name: Content control basics
585594
fileName: insert-and-change-content-controls.yaml
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
id: word-close-document-window
2+
name: Close document window
3+
description: Shows how to close document window.
4+
host: WORD
5+
api_set:
6+
WordApi: '1.10'
7+
script:
8+
content: |
9+
document.getElementById("close-with-save-prompt").addEventListener("click", () => tryCatch(closeWithSavePrompt));
10+
11+
async function closeWithSavePrompt() {
12+
await Word.run(async (context) => {
13+
// Closes the document window, prompting to save if this is a new document.
14+
const window: Word.Window = context.document.activeWindow;
15+
const closeOptions: Word.WindowCloseOptions = { saveChanges: Word.SaveConfiguration.promptToSaveChanges };
16+
console.log("About to close the document window...");
17+
window.close(closeOptions);
18+
});
19+
}
20+
21+
// Default helper for invoking an action and handling errors.
22+
async function tryCatch(callback) {
23+
try {
24+
await callback();
25+
} catch (error) {
26+
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
27+
console.error(error);
28+
}
29+
}
30+
language: typescript
31+
template:
32+
content: |-
33+
<section class="ms-Fabric ms-font-m">
34+
This sample demonstrates how to close the document window.
35+
</section>
36+
<section class="ms-Fabric samples ms-font-m">
37+
<h3>Try it out</h3>
38+
<button id="close-with-save-prompt" class="ms-Button">
39+
<span class="ms-Button-label">Close document</span>
40+
</button>
41+
</section>
42+
language: html
43+
style:
44+
content: |-
45+
section.samples {
46+
margin-top: 20px;
47+
}
48+
49+
section.samples .ms-Button, section.setup .ms-Button {
50+
display: block;
51+
margin-bottom: 5px;
52+
margin-left: 20px;
53+
min-width: 80px;
54+
}
55+
language: css
56+
libraries: |-
57+
https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
58+
https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts
59+
60+
https://unpkg.com/office-ui-fabric-core@11.1.0/dist/css/fabric.min.css
61+
https://unpkg.com/office-ui-fabric-js@1.5.0/dist/css/fabric.components.min.css
162 Bytes
Binary file not shown.

snippet-extractor-output/snippets.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27483,6 +27483,19 @@ Word.SaveBehavior:enum:
2748327483
context.document.save(Word.SaveBehavior.prompt);
2748427484
await context.sync();
2748527485
});
27486+
Word.SaveConfiguration:enum:
27487+
- >-
27488+
// Link to full sample:
27489+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/close-document-window.yaml
27490+
27491+
27492+
await Word.run(async (context) => {
27493+
// Closes the document window, prompting to save if this is a new document.
27494+
const window: Word.Window = context.document.activeWindow;
27495+
const closeOptions: Word.WindowCloseOptions = { saveChanges: Word.SaveConfiguration.promptToSaveChanges };
27496+
console.log("About to close the document window...");
27497+
window.close(closeOptions);
27498+
});
2748627499
Word.Section#getFooter:member(2):
2748727500
- >-
2748827501
// Link to full sample:
@@ -29038,6 +29051,19 @@ Word.Window:class:
2903829051
console.log("First paragraph's text:", pagesFirstParagraphText[i].text);
2903929052
}
2904029053
});
29054+
Word.Window#close:member(1):
29055+
- >-
29056+
// Link to full sample:
29057+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/close-document-window.yaml
29058+
29059+
29060+
await Word.run(async (context) => {
29061+
// Closes the document window, prompting to save if this is a new document.
29062+
const window: Word.Window = context.document.activeWindow;
29063+
const closeOptions: Word.WindowCloseOptions = { saveChanges: Word.SaveConfiguration.promptToSaveChanges };
29064+
console.log("About to close the document window...");
29065+
window.close(closeOptions);
29066+
});
2904129067
Word.Window#activePane:member:
2904229068
- >-
2904329069
// Link to full sample:
@@ -29097,6 +29123,19 @@ Word.Window#panes:member:
2909729123
const panes: Word.PaneCollection = activeWindow.panes;
2909829124
console.log(`Number of panes in the current document window: ${panes.items.length}`);
2909929125
});
29126+
Word.WindowCloseOptions:interface:
29127+
- >-
29128+
// Link to full sample:
29129+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/close-document-window.yaml
29130+
29131+
29132+
await Word.run(async (context) => {
29133+
// Closes the document window, prompting to save if this is a new document.
29134+
const window: Word.Window = context.document.activeWindow;
29135+
const closeOptions: Word.WindowCloseOptions = { saveChanges: Word.SaveConfiguration.promptToSaveChanges };
29136+
console.log("About to close the document window...");
29137+
window.close(closeOptions);
29138+
});
2910029139
Word.WindowCollection:class:
2910129140
- >-
2910229141
// Link to full sample:

view-prod/word.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"word-scenarios-doc-assembly": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/90-scenarios/doc-assembly.yaml",
5959
"word-scenarios-multiple-property-set": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/90-scenarios/multiple-property-set.yaml",
6060
"word-scenarios-correlated-objects-pattern": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/90-scenarios/correlated-objects-pattern.yaml",
61+
"word-close-document-window": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/close-document-window.yaml",
6162
"word-insert-and-change-content-controls": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/insert-and-change-content-controls.yaml",
6263
"word-manage-comments": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/99-preview-apis/manage-comments.yaml"
6364
}

view/word.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"word-scenarios-doc-assembly": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/word/90-scenarios/doc-assembly.yaml",
5959
"word-scenarios-multiple-property-set": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/word/90-scenarios/multiple-property-set.yaml",
6060
"word-scenarios-correlated-objects-pattern": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/word/90-scenarios/correlated-objects-pattern.yaml",
61+
"word-close-document-window": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/word/99-preview-apis/close-document-window.yaml",
6162
"word-insert-and-change-content-controls": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/word/99-preview-apis/insert-and-change-content-controls.yaml",
6263
"word-manage-comments": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/word/99-preview-apis/manage-comments.yaml"
6364
}

0 commit comments

Comments
 (0)