Problem Creating a Textbox on PowerPoint Addin Slide Using JavaScript API

41 views Asked by At

I am trying to create a PowerPoint Addin and would like to dynamically create and format a textbox on an active slide when a button is clicked. I have done a lot of research and debugging but I am not sure why my code is not creating the textbox. I am using PowerPoint JavaScript API on VS Code Here is my sample code

function createDynamicTextBox() {
// Get the active slide
Office.context.document.getSelectedDataAsync(Office.CoercionType.SlideRange, function (result) {
  if (result.status === Office.AsyncResultStatus.Succeeded) {
    // Get the first slide
    var slide = result.value.slides[0];

    // Create a text box
    var textBox = slide.shapes.addTextbox(Office.CoercionType.Text, 100, 100, 200, 50);

    // Set properties of the text box
    textBox.textFrame.textRange.text = "Dynamic Text Box";
    textBox.textFrame.textRange.font.size = 14;

    // You can further customize the properties as needed
  } else {
   console.error("Error getting selected data: " + result.error.message);
  }
});

The problem is with the shapes object which on debug returns 'undefined'. I am wondering whether it is possible to achieve what I would like.

1

There are 1 answers

0
Eugene Astafiev On

It makes sense to call the await context.sync(); when you would like to apply your changes.

Be aware, there is an active issue with PowerPoint shapes, see Powerpoint ScriptLab Insert shape, line and text box sample does not work on web; works fine on desktop for more information.