Add and delete slides in PowerPoint
A PowerPoint add-in can add slides to the presentation and optionally specify which slide master, and which layout of the master, is used for the new slide. The add-in can also delete slides.
The APIs for adding slides are primarily used in scenarios where the IDs of the slide masters and layouts in the presentation are known at coding time or can be found in a data source at runtime. In such a scenario, either you or the customer must create and maintain a data source that correlates the selection criterion (such as the names or images of slide masters and layouts) with the IDs of the slide masters and layouts. The APIs can also be used in scenarios where the user can insert slides that use the default slide master and the master's default layout, and in scenarios where the user can select an existing slide and create a new one with the same slide master and layout (but not the same content). See Selecting which slide master and layout to use for more information about this.
Add a slide with SlideCollection.add
Add slides with the SlideCollection.add method. The following is a simple example in which a slide that uses the presentation's default slide master and the first layout of that master is added. The method always adds new slides to the end of the presentation. The following is an example.
async function addSlide() {
await PowerPoint.run(async function(context) {
context.presentation.slides.add();
await context.sync();
});
}
Select which slide master and layout to use
Use the AddSlideOptions parameter to control which slide master is used for the new slide and which layout within the master is used. The following is an example. About this code, note:
- You can include either or both the properties of the
AddSlideOptions
object. - If both properties are used, then the specified layout must belong to the specified master or an error is thrown.
- If the
masterId
property isn't present (or its value is an empty string), then the default slide master is used and thelayoutId
must be a layout of that slide master. - The default slide master is the slide master used by the last slide in the presentation. (In the unusual case where there are currently no slides in the presentation, then the default slide master is the first slide master in the presentation.)
- If the
layoutId
property isn't present (or its value is an empty string), then the first layout of the master that is specified by themasterId
is used. - Both properties are strings of one of three possible forms: nnnnnnnnnn#, #mmmmmmmmm, or nnnnnnnnnn#mmmmmmmmm, where nnnnnnnnnn is the master's or layout's ID (typically 10 digits) and mmmmmmmmm is the master's or layout's creation ID (typically 6 - 10 digits). Some examples are
2147483690#2908289500
,2147483690#
, and#2908289500
.
async function addSlide() {
await PowerPoint.run(async function(context) {
context.presentation.slides.add({
slideMasterId: "2147483690#2908289500",
layoutId: "2147483691#2499880"
});
await context.sync();
});
}
There is no practical way that users can discover the ID or creation ID of a slide master or layout. For this reason, you can really only use the AddSlideOptions
parameter when either you know the IDs at coding time or your add-in can discover them at runtime. Because users can't be expected to memorize the IDs, you also need a way to enable the user to select slides, perhaps by name or by an image, and then correlate each title or image with the slide's ID.
Accordingly, the AddSlideOptions
parameter is primarily used in scenarios in which the add-in is designed to work with a specific set of slide masters and layouts whose IDs are known. In such a scenario, either you or the customer must create and maintain a data source that correlates a selection criterion (such as slide master and layout names or images) with the corresponding IDs or creation IDs.
Have the user choose a matching slide
If your add-in can be used in scenarios where the new slide should use the same combination of slide master and layout that is used by an existing slide, then your add-in can (1) prompt the user to select a slide and (2) read the IDs of the slide master and layout. The following steps show how to read the IDs and add a slide with a matching master and layout.
Create a function to get the index of the selected slide. The following is an example. About this code, note:
- It uses the Office.context.document.getSelectedDataAsync method of the Common JavaScript APIs.
- The call to
getSelectedDataAsync
is embedded in a Promise-returning function. For more information about why and how to do this, see Wrap Common APIs in promise-returning functions. getSelectedDataAsync
returns an array because multiple slides can be selected. In this scenario, the user has selected just one, so the code gets the first (0th) slide, which is the only one selected.- The
index
value of the slide is the 1-based value the user sees beside the slide in the thumbnails pane.
function getSelectedSlideIndex() { return new OfficeExtension.Promise<number>(function(resolve, reject) { Office.context.document.getSelectedDataAsync(Office.CoercionType.SlideRange, function(asyncResult) { try { if (asyncResult.status === Office.AsyncResultStatus.Failed) { reject(console.error(asyncResult.error.message)); } else { resolve(asyncResult.value.slides[0].index); } } catch (error) { reject(console.log(error)); } }); }); }
Call your new function inside the PowerPoint.run() of the main function that adds the slide. The following is an example.
async function addSlideWithMatchingLayout() { await PowerPoint.run(async function(context) { let selectedSlideIndex = await getSelectedSlideIndex(); // Decrement the index because the value returned by getSelectedSlideIndex() // is 1-based, but SlideCollection.getItemAt() is 0-based. const realSlideIndex = selectedSlideIndex - 1; const selectedSlide = context.presentation.slides.getItemAt(realSlideIndex).load("slideMaster/id, layout/id"); await context.sync(); context.presentation.slides.add({ slideMasterId: selectedSlide.slideMaster.id, layoutId: selectedSlide.layout.id }); await context.sync(); }); }
Delete slides
Delete a slide by getting a reference to the Slide object that represents the slide and call the Slide.delete
method. The following is an example in which the 4th slide is deleted.
async function deleteSlide() {
await PowerPoint.run(async function(context) {
// The slide index is zero-based.
const slide = context.presentation.slides.getItemAt(3);
slide.delete();
await context.sync();
});
}
Office Add-ins