Edit

Share via


Add and delete PowerPoint slides programmatically

Quickly add and remove slides from a PowerPoint presentation using an add-in.

  • Add slides programmatically with SlideCollection.add() and control master and layout when needed.
  • Match an existing slide's master or layout, or choose specific slideMasterId and layoutId at runtime.
  • Delete slides by reference or index; always include await context.sync() after changes.
  • Estimated time: Try the short examples in ~2–5 minutes.

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 an example where 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.

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 the layoutId 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 the masterId 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's 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.

  1. 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));
                }
            });
        });
    }
    
  2. 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 then calling the Slide.delete method. The following is an example where 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();
    });
}

Considerations

  • Master/layout IDs are opaque: slideMasterId and layoutId are ID strings (often long numeric tokens). There's no UI for users to discover them, so your add-in should map friendly names or thumbnails to IDs at runtime.
  • Index differences: Office.context.document.getSelectedDataAsync(Office.CoercionType.SlideRange) returns 1-based slide indexes. slides.getItemAt(...) is 0-based—subtract 1 when using the value.
  • Call context.sync() after mutations: Adding or deleting slides takes effect on the document; always include await context.sync() after calling add() or delete() to ensure changes are applied.
  • Layout must belong to master: If you supply both slideMasterId and layoutId, the layout must belong to that master or an error is thrown.
  • Work on copies for bulk changes: Deleting slides is immediate. Consider prompting users or operating on a copy when performing batch deletes.