Search code examples
angularvbatypescriptpowerpointoffice-addins

How to add an image to a slide


I'm working on a PowerPoint add-in. I want to add an image to a slide.

I'm using the Angular framework with Typescript for the add-in and generated the add-in using the Yeoman Office generator.

This is the function I call when a button is clicked.

async addImage() {
    console.log('addImage')
    
    Office.context.document.setSelectedDataAsync("addImage",
      {
        coercionType: Office.CoercionType.Text,
      },
      result => {
        if (result.status === Office.AsyncResultStatus.Failed) {
          console.error(result.error.message);
        }
      }
    );

  }

It adds text. I want to replace this so it can add an image instead.


Solution

  • I found an answer after some hardcore searching. I had to use a base64 string and add it in to the PowerPoint like this:

    async addImage(imgHTML) {
        console.log('addImage')
    
        Office.context.document.setSelectedDataAsync('addBase64StringHere', 
          { 
            coercionType: Office.CoercionType.Image, 
          },
          result => {
            if (result.status === Office.AsyncResultStatus.Failed) {
              console.error(result.error.message);
            }
          }
        );
    
      }