javascriptpowerpointoffice-jspowerpoint-addins

Insert slide(s) before or after currently selected slide in PowerPoint


I have a function that fetches an API, that converts PowerPoint presentations into base64 strings. After getting the response object, I want to insert the slides into the current presentation, specifically after the currently selected slide.

The way I thought of doing it is what the documentation says: get the selected slide ID and use it in the target slide ID option. The documentation introduces a function, getSelectedSlideIndex(), to get the ID of the selected slide, but it throws a Rich API Promise error. Reference: https://learn.microsoft.com/en-us/office/dev/add-ins/powerpoint/add-slides

export async function getImageAsBase64String() {
  // API call that fetches presentations as base64 strings
  // ..
  const results = await response.json();

  await PowerPoint.run(async (context) => {
    const selectedID = await getSelectedSlideIndex();

    context.presentation.insertSlidesFromBase64(results[0].presentations[0], {  
      targetSlideId: selectedID,
    });          
      await context.sync();
  });
}

Is there a specific format for the selected ID? Ideally, I want to select a slide, press a button and the new slides (as base64 strings) are inserted after the selected slide.


Solution

  • (Turning a comment into an answer)

    Use the getSelectedSlideID method for inserting slides as described in Insert slides.

    The getSelectedSlideIndex method is for adding slides.