PowerPoint.Presentation class

Extends

Remarks

[ API set: PowerPointApi 1.0 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/insert-slides.yaml

await PowerPoint.run(async function(context) {
    // Get the ID of the first selected slide.
    const presentation: PowerPoint.Presentation = context.presentation;
    const selected: PowerPoint.Slide = presentation.getSelectedSlides().getItemAt(0);
    selected.load("id");
    await context.sync();

    // Insert the other presentation after the selected slide.
    const insertOptions: PowerPoint.InsertSlideOptions = {
        formatting: PowerPoint.InsertSlideFormatting.useDestinationTheme,
        targetSlideId: selected.id
    };
    presentation.insertSlidesFromBase64(chosenFileBase64, insertOptions);
    await context.sync();
});

Properties

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

id

Gets the ID of the presentation.

slideMasters

Returns the collection of SlideMaster objects that are in the presentation.

slides

Returns an ordered collection of slides in the presentation.

tags

Returns a collection of tags attached to the presentation.

title

Methods

getSelectedShapes()

Returns the selected shapes in the current slide of the presentation. If no shapes are selected, an empty collection is returned.

getSelectedSlides()

Returns the selected slides in the current view of the presentation. The first item in the collection is the active slide that is visible in the editing area. If no slides are selected, an empty collection is returned.

getSelectedTextRange()

Returns the selected PowerPoint.TextRange in the current view of the presentation. Throws an exception if no text is selected.

getSelectedTextRangeOrNullObject()

Returns the selected PowerPoint.TextRange in the current view of the presentation. If no text is selected, an object with an isNullObject property set to true is returned.

insertSlidesFromBase64(base64File, options)

Inserts the specified slides from a presentation into the current presentation.

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

setSelectedSlides(slideIds)

Selects the slides in the current view of the presentation. Existing slide selection is replaced with the new selection.

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original PowerPoint.Presentation object is an API object, the toJSON method returns a plain JavaScript object (typed as PowerPoint.Interfaces.PresentationData) that contains shallow copies of any loaded child properties from the original object.

Property Details

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

context: RequestContext;

Property Value

id

Gets the ID of the presentation.

readonly id: string;

Property Value

string

Remarks

[ API set: PowerPointApi 1.5 ]

slideMasters

Returns the collection of SlideMaster objects that are in the presentation.

readonly slideMasters: PowerPoint.SlideMasterCollection;

Property Value

Remarks

[ API set: PowerPointApi 1.3 ]

slides

Returns an ordered collection of slides in the presentation.

readonly slides: PowerPoint.SlideCollection;

Property Value

Remarks

[ API set: PowerPointApi 1.2 ]

tags

Returns a collection of tags attached to the presentation.

readonly tags: PowerPoint.TagCollection;

Property Value

Remarks

[ API set: PowerPointApi 1.3 ]

title

readonly title: string;

Property Value

string

Method Details

getSelectedShapes()

Returns the selected shapes in the current slide of the presentation. If no shapes are selected, an empty collection is returned.

getSelectedShapes(): PowerPoint.ShapeScopedCollection;

Returns

Remarks

[ API set: PowerPointApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/get-set-shapes.yaml

// Arranges the selected shapes in a line from left to right.
await PowerPoint.run(async (context) => {
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  shapes.load("items");
  await context.sync();
  let maxHeight = 0;
  shapes.items.map((shape) => {
    shape.load("width,height");
  });
  await context.sync();
  shapes.items.map((shape) => {
    shape.left = currentLeft;
    shape.top = currentTop;
    currentLeft += shape.width;
    if (shape.height > maxHeight) maxHeight = shape.height;
  });
  await context.sync();
  currentLeft = 0;
  if (currentTop > slideHeight - 200) currentTop = 0;
});

...

// Gets the shapes you selected on the slide and displays their IDs on the task pane.
await PowerPoint.run(async (context) => {
  let finalTable = "";
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  await context.sync();
  finalTable += "<br>getSelectedShapes.getCount returned:<b>" + shapeCount.value + "</b><br>";
  finalTable +=
    "<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  shapes.load("items");
  await context.sync();
  shapes.items.map((shape, index) => {
    finalTable += "<tr><td>" + index + "</td><td>" + shape.id + "</td></tr>";
  });
  finalTable += "</table>";
  $("#outputSpan").empty();
  $("#outputSpan").append(finalTable);
});

...

// Saves which shapes are selected so that they can be reselected later.
await PowerPoint.run(async (context) => {
  context.presentation.load("slides");
  await context.sync();
  const slides: PowerPoint.SlideScopedCollection = context.presentation.getSelectedSlides();
  const slideCount = slides.getCount();
  slides.load("items");
  await context.sync();
  savedSlideSelection = [];
  slides.items.map((slide) => {
    savedSlideSelection.push(slide.id);
  });
  const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  shapes.load("items");
  await context.sync();
  shapes.items.map((shape) => {
    savedShapeSelection.push(shape.id);
  });
});

getSelectedSlides()

Returns the selected slides in the current view of the presentation. The first item in the collection is the active slide that is visible in the editing area. If no slides are selected, an empty collection is returned.

getSelectedSlides(): PowerPoint.SlideScopedCollection;

Returns

Remarks

[ API set: PowerPointApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/get-set-slides.yaml

// Gets the selected slides and displays their IDs on the task pane.
await PowerPoint.run(async (context) => {
  let finalTable = "";
  context.presentation.load("slides");
  await context.sync();
  const allSlidesList = {};
  const allSlidesCount = context.presentation.slides.getCount();
  context.presentation.slides.load("items");
  await context.sync();
  let allSlideItems: PowerPoint.Slide[] = context.presentation.slides.items;
  allSlideItems.map((slide, index) => {
    allSlidesList[slide.id] = `Slide ${index + 1}`;
  });

  if ($("#id-check-usenative").is(":checked")) {
    context.presentation.load("tags");
  }
  const slides: PowerPoint.SlideScopedCollection = context.presentation.getSelectedSlides();
  const slideCount = slides.getCount();
  slides.load("items");
  await context.sync();
  finalTable += "<br>getSelectedSlides.getCount returned:<b>" + slideCount.value + "</b><br>";
  finalTable +=
    "<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  slides.items.map((slide, index) => {
    finalTable += "<tr><td>" + index + " - " + allSlidesList[slide.id] + "</td><td>" + slide.id + "</td></tr>";
  });
  finalTable += "</table>";
  $("#outputSpan").empty();
  $("#outputSpan").append(finalTable);
});

...

// Saves which slides are currently selected so they can be reselected later.
await PowerPoint.run(async (context) => {
  let finalTable = "";
  context.presentation.load("slides");
  await context.sync();
  const slides: PowerPoint.SlideScopedCollection = context.presentation.getSelectedSlides();
  const slideCount = slides.getCount();
  await context.sync();
  finalTable += "<br>getSelectedSlides.getCount returned:<b>" + slideCount.value + "</b><br>";
  finalTable +=
    "<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  savedSlideSelection = [];
  slides.load("items");
  await context.sync();
  slides.items.map((slide, index) => {
    finalTable += "<tr><td>" + index + "</td><td>" + slide.id + "</td></tr>";
    savedSlideSelection.push(slide.id);
  });
  finalTable += "</table>";
  $("#outputSpan").empty();
  $("#outputSpan").append(finalTable);
});

getSelectedTextRange()

Returns the selected PowerPoint.TextRange in the current view of the presentation. Throws an exception if no text is selected.

getSelectedTextRange(): PowerPoint.TextRange;

Returns

Remarks

[ API set: PowerPointApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/text/get-set-textrange.yaml

// Gets the selected text range and prints data about the range on the task pane.
await PowerPoint.run(async (context) => {
  const textRange: PowerPoint.TextRange = context.presentation.getSelectedTextRange();
  try {
    await context.sync();
  } catch (error) {
    console.warn("You must select only one range of text for this action to work.");
    return;
  }
  textRange.load("text");
  textRange.load("start");
  textRange.load("length");
  await context.sync();
  let txtHtml = textRange.text;
  txtHtml = txtHtml.replace(/\n/g, "<br>");
  txtHtml = txtHtml.replace(/\r/g, "<br>");
  txtHtml = txtHtml.replace(/\v/g, "<br>");
  let txtExplained = textRange.text;
  txtExplained = txtExplained.replace(/\n/g, "<font color=red>NL</font>");
  txtExplained = txtExplained.replace(/\r/g, "<font color=red>CR</font>");
  txtExplained = txtExplained.replace(/\v/g, "<font color=red>VV</font>");
  let finalTable = "";
  finalTable +=
    "<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
  finalTable += "<tr><td>Raw</td><td>" + textRange.text + "</td></tr>";
  finalTable += "<tr><td>Html</td><td>" + txtHtml + "</td></tr>";
  finalTable += "<tr><td>Exp</td><td>" + txtExplained + "</td></tr>";
  finalTable += "<tr><td>Start</td><td>" + textRange.start + "</td></tr>";
  finalTable += "<tr><td>Length</td><td>" + textRange.length + "</td></tr>";
  finalTable += "</table>";
  $("#outputSpan").empty();
  $("#outputSpan").append(finalTable);
});

...

// Sets the range selection to the range that was saved previously.
await PowerPoint.run(async (context) => {
  const slide1: PowerPoint.Slide = context.presentation.slides.getItem(savedTextSlideSelection[0]);
  const shape1: PowerPoint.Shape = slide1.shapes.getItem(savedTextShapeSelection[0]);
  const textRange: PowerPoint.TextRange = shape1.textFrame.textRange.getSubstring(savedTextTextRangeStart, savedTextTextRangeLength);
  textRange.setSelected();
  await context.sync();
});

getSelectedTextRangeOrNullObject()

Returns the selected PowerPoint.TextRange in the current view of the presentation. If no text is selected, an object with an isNullObject property set to true is returned.

getSelectedTextRangeOrNullObject(): PowerPoint.TextRange;

Returns

Remarks

[ API set: PowerPointApi 1.5 ]

insertSlidesFromBase64(base64File, options)

Inserts the specified slides from a presentation into the current presentation.

insertSlidesFromBase64(base64File: string, options?: PowerPoint.InsertSlideOptions): void;

Parameters

base64File

string

The Base64-encoded string representing the source presentation file.

options
PowerPoint.InsertSlideOptions

The options that define which slides will be inserted, where the new slides will go, and which presentation's formatting will be used.

Returns

void

Remarks

[ API set: PowerPointApi 1.2 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/insert-slides.yaml

await PowerPoint.run(async function(context) {
    // Get the ID of the first selected slide.
    const presentation: PowerPoint.Presentation = context.presentation;
    const selected: PowerPoint.Slide = presentation.getSelectedSlides().getItemAt(0);
    selected.load("id");
    await context.sync();

    // Insert the other presentation after the selected slide.
    const insertOptions: PowerPoint.InsertSlideOptions = {
        formatting: PowerPoint.InsertSlideFormatting.useDestinationTheme,
        targetSlideId: selected.id
    };
    presentation.insertSlidesFromBase64(chosenFileBase64, insertOptions);
    await context.sync();
});

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(options?: PowerPoint.Interfaces.PresentationLoadOptions): PowerPoint.Presentation;

Parameters

options
PowerPoint.Interfaces.PresentationLoadOptions

Provides options for which properties of the object to load.

Returns

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames?: string | string[]): PowerPoint.Presentation;

Parameters

propertyNames

string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): PowerPoint.Presentation;

Parameters

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

Returns

setSelectedSlides(slideIds)

Selects the slides in the current view of the presentation. Existing slide selection is replaced with the new selection.

setSelectedSlides(slideIds: string[]): void;

Parameters

slideIds

string[]

List of slide IDs to select in the presentation. If the list is empty, selection is cleared.

Returns

void

Remarks

[ API set: PowerPointApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/get-set-slides.yaml

// Sets selection to the slides that were saved.
await PowerPoint.run(async (context) => {
  context.presentation.setSelectedSlides(savedSlideSelection);
  await context.sync();
});

...

// Selects slides 2, 4, and 5.
await PowerPoint.run(async (context) => {
  context.presentation.load("slides");
  await context.sync();
  const slide2: PowerPoint.Slide = context.presentation.slides.getItemAt(1);
  const slide4: PowerPoint.Slide = context.presentation.slides.getItemAt(3);
  const slide5: PowerPoint.Slide = context.presentation.slides.getItemAt(4);
  slide2.load("id");
  slide4.load("id");
  slide5.load("id");
  try {
    await context.sync();
  } catch (error) {
    console.warn("This action requires at least 5 slides in the presentation.");
    return;
  }
  await context.sync();
  context.presentation.setSelectedSlides([slide2.id, slide4.id, slide5.id]);
  await context.sync();
});

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original PowerPoint.Presentation object is an API object, the toJSON method returns a plain JavaScript object (typed as PowerPoint.Interfaces.PresentationData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): PowerPoint.Interfaces.PresentationData;

Returns