Search code examples
office365powerpointoffice-jsoffice-addinspowerpoint-web-addins

Copy PowerPoint shapes using office js to a new slide


I'm trying to do something which seems simple using office js but I can't get it to work. I am reading a pptx file and inserting the first slide into my presentation with the following:

  //insert new slide from pptx file
  context.presentation.insertSlidesFromBase64(base64file);
  await context.sync();
  context.presentation.slides.load();
  await context.sync();

  //load all the shapes on the new slide
  const slide = context.presentation.slides.getItemAt(0);
  slide.load();
  await context.sync();

After doing this, I want to create these same loaded shapes on a new slide or cut and paste the shapes on to a new slide. I can't see any way to do this. Am I missing something or is this not possible?

I expected there would be a way to clone existing shapes and add them to a different slide but I don't see that is possible. I thought maybe you could add the cloned shapes to the slidecollection of a different slide but that doesn't seem to be possible. What I am trying to achieve is reading in a pptx file with one slide, reading the shapes of that slide and inserting those shapes on the currently selected slide in PowerPoint. The reason for this is that we really want to do is read in multiple PowerPoint files, get all the shapes and put them on one slide, preferably grouping each set of shapes from each selected input PowerPoint file.

Thanks in advance for any assistance.


Solution

  • I've consulted internally at Microsoft and the answer is that there currently isn't any way to copy images in the way that you want. I was hoping that you could just push image objects to the ShapeCollection.items array; but that won't work. You can never add objects to a collection object that way, because the Office context object in the JavaScript runtime doesn't know about them unless they are added through an Office.js method. (As you have found, there is no method on the ShapeCollection object to add images.) Since the context doesn't know about objects that are pushed to the items array, when context.sync is called, those objects are not sent from the JavaScript runtime to the open presentation document.

    Sorry for the bad news. You can suggest that an API for this scenario be added to Office.js at Microsoft 365 Developer Platform Ideas.