title | description | ms.date | ms.service | ms.localizationpriority |
---|---|---|---|---|
PowerPoint add-in tutorial |
In this tutorial, you will build a PowerPoint add-in that inserts an image, inserts text, gets slide metadata, and navigates between slides. |
09/28/2023 |
powerpoint |
high |
In this tutorial, you'll use Visual Studio Code (VS Code), Visual Studio, or your preferred code editor to create a PowerPoint task pane add-in that:
[!div class="checklist"]
- Adds an image to a slide
- Adds text to a slide
- Gets slide metadata
- Adds new slides
- Navigates between slides
[!includeChoose your editor]
Tip
If you've already completed the Build your first PowerPoint task pane add-in quick start using the Yeoman generator, and want to use that project as a starting point for this tutorial, go directly to the Insert an image section to start this tutorial.
If you want a completed version of this tutorial, head over to the Office Add-ins samples repo on GitHub.
[!includeSet up requirements] [!includeYeoman generator prerequisites]
[!includeYeoman generator create project guidance]
- Choose a project type:
Office Add-in Task Pane project
- Choose a script type:
JavaScript
- What do you want to name your add-in?
My Office Add-in
- Which Office client application would you like to support?
PowerPoint
After you complete the wizard, the generator creates the project and installs supporting Node components.
[!includeYeoman generator next steps]
-
Navigate to the root directory of the project.
cd "My Office Add-in"
-
Open your project in VS Code or your preferred code editor.
[!INCLUDE Instructions for opening add-in project in VS Code via command line]
Complete the following steps to add code that inserts an image into a slide.
-
Open the project in your code editor.
-
In the root of the project, create a new file named base64Image.js.
-
Open the file base64Image.js and add the following code to specify the Base64-encoded string that represents an image.
export const base64Image = "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEFCAIAAABCdiZrAAAACXBIWXMAAAsSAAALEgHS3X78AAAgAElEQVR42u2dzW9bV3rGn0w5wLBTRpSACAUDmDRowGoj1DdAtBA6suksZmtmV3Qj+i8w3XUB00X3pv8CX68Gswq96aKLhI5bCKiM+gpVphIa1qQBcQbyQB/hTJlpOHUXlyEvD885vLxfvCSfH7KIJVuUrnif+z7nPOd933v37h0IIWQe+BEvASGEgkUIIRQsQggFixBCKFiEEELBIoRQsAghhIJFCCEULEIIBYsQQihYhBBCwSKEULAIIYSCRQghFCxCCAWLEEIoWI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UlysL4nJuAFCwSJ9mytxEpWyNF6Ao2n2CnqZyXQShYZGasFbBV5zZiX6rsTUDmFShYJNbY24jXHy3venxmt39omZuAFCwyH2TLy7iNuH6nvwlIqaJgkXmzRcu0jWhvAho1bgJSsMg8M9hGXL+zoD9gtp9X4CYgBYssjmwZtUXbRrQPLe80KVUULLKI2NuIxudzv41obwJuW9wEpGCRRWe92O/FPKfr8VfucROQgkWWjExp/rYR7c7FG1VKFQWLLB+DXszx30a0NwF5aJlQsChb/W3EeMpW6gY3AQkFi4xipx9itY1obwJuW5QqIj5keQkIEJuRrhxfSlhhkSlka4YjXTm+lFCwyNREP9KV40sJBYv4sGY/bCNeuRfuC63ewvYrbgISChYJQrY2qmFtIw46F6cMXmlCwSIBEfhIV44vJRQsEi6BjHTl+FJCwSLR4XmkK8eXEgoWmQ3TjnTl+FJCwSIzZjDSVQPHl5JAee/du3e8CsQX3Sa6Y730pB8khIJFCKElJIQQChYhhFCwCCEULEIIoWARQggFixBCwSKEEAoWIYRQsAghFCxCCKFgEUIIBYsQQsEihBAKFiGEULAIIRQsQgihYBFCCAWLEELBIoQQChYhhILFS0AIoWARQkjA/D87uqZQTj7xTgAAAABJRU5ErkJggg==";
-
Open the file ./src/taskpane/taskpane.html. This file contains the HTML markup for the task pane.
-
Locate the
<body>
element. Replace it with the following markup, then save the file.<body class="ms-font-m ms-welcome ms-Fabric"> <!-- TODO2: Update the header node. --> <header class="ms-welcome__header ms-bgColor-neutralLighter"> <img width="90" height="90" src="../../assets/logo-filled.png" alt="Contoso" title="Contoso" /> <h1 class="ms-font-su">Welcome</h1> </header> <section id="sideload-msg" class="ms-welcome__main"> <h2 class="ms-font-xl">Please <a target="_blank" href="https://learn.microsoft.com/office/dev/add-ins/testing/test-debug-office-add-ins#sideload-an-office-add-in-for-testing">sideload</a> your add-in to see app body.</h2> </section> <main id="app-body" class="ms-welcome__main" style="display: none;"> <div class="padding"> <!-- TODO1: Create the insert-image button. --> <!-- TODO3: Create the insert-text button. --> <!-- TODO4: Create the get-slide-metadata button. --> <!-- TODO5: Create the add-slides and go-to-slide buttons. --> </div> </main> <section id="display-msg" class="ms-welcome__main"> <div class="padding"> <h3>Message</h3> <div id="message"></div> </div> </section> </body>
-
In the taskpane.html file, replace
TODO1
with the following markup. This markup defines the Insert Image button that will appear within the add-in's task pane.<button class="ms-Button" id="insert-image">Insert Image</button><br/><br/>
-
Open the file ./src/taskpane/taskpane.js. This file contains the Office JavaScript API code that facilitates interaction between the task pane and the Office client application. Replace the entire contents with the following code and save the file.
/* * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. * See LICENSE in the project root for license information. */ /* global document, Office */ // TODO1: Import Base64-encoded string for image. Office.onReady((info) => { if (info.host === Office.HostType.PowerPoint) { document.getElementById("sideload-msg").style.display = "none"; document.getElementById("app-body").style.display = "flex"; // TODO2: Assign event handler for insert-image button. // TODO4: Assign event handler for insert-text button. // TODO6: Assign event handler for get-slide-metadata button. // TODO8: Assign event handlers for add-slides and the four navigation buttons. } }); // TODO3: Define the insertImage function. // TODO5: Define the insertText function. // TODO7: Define the getSlideMetadata function. // TODO9: Define the addSlides and navigation functions. async function clearMessage(callback) { document.getElementById("message").innerText = ""; await callback(); } function setMessage(message) { document.getElementById("message").innerText = message; } // Default helper for invoking an action and handling errors. async function tryCatch(callback) { try { document.getElementById("message").innerText = ""; await callback(); } catch (error) { setMessage("Error: " + error.toString()); } }
-
In the taskpane.js file above the
Office.onReady
function call near the top of the file, replaceTODO1
with the following code. This code imports the variable that you defined previously in the file ./base64Image.js.import { base64Image } from "../../base64Image";
-
In the taskpane.js file, replace
TODO2
with the following code to assign the event handler for the Insert Image button.document.getElementById("insert-image").onclick = () => clearMessage(insertImage);
-
In the taskpane.js file, replace
TODO3
with the following code to define theinsertImage
function. This function uses the Office JavaScript API to insert the image into the document. Note:-
The
coercionType
option that's specified as the second parameter of thesetSelectedDataAsync
request indicates the type of data being inserted. -
The
asyncResult
object encapsulates the result of thesetSelectedDataAsync
request, including status and error information if the request failed.
function insertImage() { // Call Office.js to insert the image into the document. Office.context.document.setSelectedDataAsync( base64Image, { coercionType: Office.CoercionType.Image }, (asyncResult) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { setMessage("Error: " + asyncResult.error.message); } } ); }
-
-
Save all your changes to the project.
-
Navigate to the root folder of the project.
cd "My Office Add-in"
-
Complete the following steps to start the local web server and sideload your add-in.
[!INCLUDE alert use https]
[!TIP] If you're testing your add-in on Mac, run the following command before proceeding. When you run this command, the local web server starts.
npm run dev-server
-
To test your add-in in PowerPoint, run the following command in the root directory of your project. This starts the local web server (if it's not already running) and opens PowerPoint with your add-in loaded.
npm start
-
To test your add-in in PowerPoint on a browser, run the following command in the root directory of your project. When you run this command, the local web server starts. Replace "{url}" with the URL of a PowerPoint document on your OneDrive or a SharePoint library to which you have permissions.
[!INCLUDE npm start:web command syntax]
-
-
If the add-in task pane isn't already open in PowerPoint, choose the Home tab, and then choose the Show Taskpane button on the ribbon to open the add-in task pane.
-
In the task pane, choose the Insert Image button to add the image to the current slide.
Complete the following steps to add markup that customizes the task pane UI.
-
In the taskpane.html file, replace
TODO2
and the current header section with the following markup to update the header section and title in the task pane. Note:- The styles that begin with
ms-
are defined by Fabric Core in Office Add-ins, a JavaScript front-end framework for building user experiences for Office. The taskpane.html file includes a reference to the Fabric Core stylesheet.
<header id="content-header"> <div class="ms-Grid ms-bgColor-neutralPrimary"> <div class="ms-Grid-row"> <div class="padding ms-Grid-col ms-u-sm12 ms-u-md12 ms-u-lg12"> <div class="ms-font-xl ms-fontColor-white ms-fontWeight-semibold">My PowerPoint add-in</div></div> </div> </div> </header>
- The styles that begin with
-
Save all your changes to the project.
-
If the local web server isn't already running, complete the following steps to start the local web server and sideload your add-in.
[!INCLUDE alert use https]
[!TIP] If you're testing your add-in on Mac, run the following command before proceeding. When you run this command, the local web server starts.
npm run dev-server
-
To test your add-in in PowerPoint, run the following command in the root directory of your project. This starts the local web server (if it's not already running) and opens PowerPoint with your add-in loaded.
npm start
-
To test your add-in in PowerPoint on a browser, run the following command in the root directory of your project. When you run this command, the local web server starts. Replace "{url}" with the URL of a PowerPoint document on your OneDrive or a SharePoint library to which you have permissions.
[!INCLUDE npm start:web command syntax]
-
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
Notice that the task pane now contains an updated header section and title.
Complete the following steps to add code that inserts text into the title slide which contains an image.
-
In the taskpane.html file, replace
TODO3
with the following markup. This markup defines the Insert Text button that will appear within the add-in's task pane.<button class="ms-Button" id="insert-text">Insert Text</button><br/><br/>
-
In the taskpane.js file, replace
TODO4
with the following code to assign the event handler for the Insert Text button.document.getElementById("insert-text").onclick = () => clearMessage(insertText);
-
In the taskpane.js file, replace
TODO5
with the following code to define theinsertText
function. This function inserts text into the current slide.function insertText() { Office.context.document.setSelectedDataAsync("Hello World!", (asyncResult) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { setMessage("Error: " + asyncResult.error.message); } }); }
-
Save all your changes to the project.
-
Navigate to the root folder of the project.
cd "My Office Add-in"
-
If the local web server isn't already running, complete the following steps to start the local web server and sideload your add-in.
[!INCLUDE alert use https]
[!TIP] If you're testing your add-in on Mac, run the following command before proceeding. When you run this command, the local web server starts.
npm run dev-server
-
To test your add-in in PowerPoint, run the following command in the root directory of your project. This starts the local web server (if it's not already running) and opens PowerPoint with your add-in loaded.
npm start
-
To test your add-in in PowerPoint on a browser, run the following command in the root directory of your project. When you run this command, the local web server starts. Replace "{url}" with the URL of a PowerPoint document on your OneDrive or a SharePoint library to which you have permissions.
[!INCLUDE npm start:web command syntax]
-
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
In the task pane, choose the Insert Image button to add the image to the current slide, then choose a design for the slide that contains a text box for the title.
-
Put your cursor in the text box on the title slide and then in the task pane, choose the Insert Text button to add text to the slide.
Complete the following steps to add code that retrieves metadata for the selected slide.
-
In the taskpane.html file, replace
TODO4
with the following markup. This markup defines the Get Slide Metadata button that will appear within the add-in's task pane.<button class="ms-Button" id="get-slide-metadata">Get Slide Metadata</button><br/><br/>
-
In the taskpane.js file, replace
TODO6
with the following code to assign the event handler for the Get Slide Metadata button.document.getElementById("get-slide-metadata").onclick = () => clearMessage(getSlideMetadata);
-
In the taskpane.js file, replace
TODO7
with the following code to define thegetSlideMetadata
function. This function retrieves metadata for the selected slides and writes it to the Message section in the add-in task pane.function getSlideMetadata() { Office.context.document.getSelectedDataAsync(Office.CoercionType.SlideRange, (asyncResult) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { setMessage("Error: " + asyncResult.error.message); } else { setMessage("Metadata for selected slides: " + JSON.stringify(asyncResult.value)); } }); }
-
Save all your changes to the project.
-
Navigate to the root folder of the project.
cd "My Office Add-in"
-
If the local web server isn't already running, complete the following steps to start the local web server and sideload your add-in.
[!INCLUDE alert use https]
[!TIP] If you're testing your add-in on Mac, run the following command before proceeding. When you run this command, the local web server starts.
npm run dev-server
-
To test your add-in in PowerPoint, run the following command in the root directory of your project. This starts the local web server (if it's not already running) and opens PowerPoint with your add-in loaded.
npm start
-
To test your add-in in PowerPoint on a browser, run the following command in the root directory of your project. When you run this command, the local web server starts. Replace "{url}" with the URL of a PowerPoint document on your OneDrive or a SharePoint library to which you have permissions.
[!INCLUDE npm start:web command syntax]
-
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
In the task pane, choose the Get Slide Metadata button to get the metadata for the selected slide. The slide metadata is written in the Message section below the buttons in the task pane. In this case, the
slides
array within the JSON metadata contains one object that specifies theid
,title
, andindex
of the selected slide. If multiple slides had been selected when you retrieved slide metadata, theslides
array within the JSON metadata would contain one object for each selected slide.
Complete the following steps to add code that navigates between the slides of a document.
-
In the taskpane.html file, replace
TODO5
with the following markup. This markup defines the four navigation buttons that will appear within the add-in's task pane.<button class="ms-Button" id="add-slides">Add Slides</button><br/><br/> <button class="ms-Button" id="go-to-first-slide">Go to First Slide</button><br/><br/> <button class="ms-Button" id="go-to-next-slide">Go to Next Slide</button><br/><br/> <button class="ms-Button" id="go-to-previous-slide">Go to Previous Slide</button><br/><br/> <button class="ms-Button" id="go-to-last-slide">Go to Last Slide</button><br/><br/>
-
In the taskpane.js file, replace
TODO8
with the following code to assign the event handlers for the Add Slides and four navigation buttons.document.getElementById("add-slides").onclick = () => tryCatch(addSlides); document.getElementById("go-to-first-slide").onclick = () => clearMessage(goToFirstSlide); document.getElementById("go-to-next-slide").onclick = () => clearMessage(goToNextSlide); document.getElementById("go-to-previous-slide").onclick = () => clearMessage(goToPreviousSlide); document.getElementById("go-to-last-slide").onclick = () => clearMessage(goToLastSlide);
-
In the taskpane.js file, replace
TODO9
with the following code to define theaddSlides
and navigation functions. Each of these functions uses thegoToByIdAsync
method to select a slide based upon its position in the document (first, last, previous, and next).async function addSlides() { await PowerPoint.run(async function (context) { context.presentation.slides.add(); context.presentation.slides.add(); await context.sync(); goToLastSlide(); setMessage("Success: Slides added."); }); } function goToFirstSlide() { Office.context.document.goToByIdAsync(Office.Index.First, Office.GoToType.Index, (asyncResult) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { setMessage("Error: " + asyncResult.error.message); } }); } function goToLastSlide() { Office.context.document.goToByIdAsync(Office.Index.Last, Office.GoToType.Index, (asyncResult) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { setMessage("Error: " + asyncResult.error.message); } }); } function goToPreviousSlide() { Office.context.document.goToByIdAsync(Office.Index.Previous, Office.GoToType.Index, (asyncResult) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { setMessage("Error: " + asyncResult.error.message); } }); } function goToNextSlide() { Office.context.document.goToByIdAsync(Office.Index.Next, Office.GoToType.Index, (asyncResult) => { if (asyncResult.status === Office.AsyncResultStatus.Failed) { setMessage("Error: " + asyncResult.error.message); } }); }
-
Save all your changes to the project.
-
Navigate to the root folder of the project.
cd "My Office Add-in"
-
If the local web server isn't already running, complete the following steps to start the local web server and sideload your add-in.
[!INCLUDE alert use https]
[!TIP] If you're testing your add-in on Mac, run the following command before proceeding. When you run this command, the local web server starts.
npm run dev-server
-
To test your add-in in PowerPoint, run the following command in the root directory of your project. This starts the local web server (if it's not already running) and opens PowerPoint with your add-in loaded.
npm start
-
To test your add-in in PowerPoint on a browser, run the following command in the root directory of your project. When you run this command, the local web server starts. Replace "{url}" with the URL of a PowerPoint document on your OneDrive or a SharePoint library to which you have permissions.
[!INCLUDE npm start:web command syntax]
-
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
In the task pane, choose the Add Slides button. Two new slides are added to the document and the last slide in the document is selected and displayed.
-
In the task pane, choose the Go to First Slide button. The first slide in the document is selected and displayed.
-
In the task pane, choose the Go to Next Slide button. The next slide in the document is selected and displayed.
-
In the task pane, choose the Go to Previous Slide button. The previous slide in the document is selected and displayed.
-
In the task pane, choose the Go to Last Slide button. The last slide in the document is selected and displayed.
-
If the web server is running, run the following command when you want to stop the server.
npm stop
- Completed PowerPoint add-in tutorial: The result of completing this tutorial.
Tip
If you want a completed version of this tutorial, head over to the Office Add-ins samples repo on GitHub.
-
Visual Studio 2019 or later, with the Office/SharePoint development workload installed.
[!NOTE] If you've previously installed Visual Studio, use the Visual Studio Installer to ensure that the Office/SharePoint development workload is installed.
-
Office connected to a Microsoft 365 subscription (including Office on the web).
[!NOTE] If you don't already have Office, you can join the Microsoft 365 developer program to get a free, 90-day renewable Microsoft 365 subscription to use during development.
Complete the following steps to create a PowerPoint add-in project using Visual Studio.
-
Choose Create a new project.
-
Using the search box, enter add-in. Choose PowerPoint Web Add-in, then select Next.
-
Name the project
HelloWorld
, and select Create. -
In the Create Office Add-in dialog window, choose Add new functionalities to PowerPoint, and then choose Finish to create the project.
-
Visual Studio creates a solution and its two projects appear in Solution Explorer. The Home.html file opens in Visual Studio.
-
The following NuGet packages must be installed. Install them on the HelloWorldWeb project using the NuGet Package Manager in Visual Studio. See Visual Studio help for instructions. The second of these may be installed automatically when you install the first.
- Microsoft.AspNet.WebApi.WebHost
- Microsoft.AspNet.WebApi.Core
[!IMPORTANT] When you're using the NuGet Package Manager to install these packages, do not install the recommended update to jQuery. The jQuery version installed with your Visual Studio solution matches the jQuery call within the solution files.
-
Use the NuGet Package Manager to update the Newtonsoft.Json package to version 13.0.3 or later. Then delete the app.config file if it was added to the HelloWorld project.
[!includeDescription of Visual Studio projects]
Edit the add-in code as follows to create the framework that you'll use to implement add-in functionality in subsequent steps of this tutorial.
-
Home.html specifies the HTML that will be rendered in the add-in's task pane. In Home.html, find the div with
id="content-main"
, replace that entire div with the following markup, and save the file.<!-- TODO2: Create the content-header div. --> <div id="content-main"> <div class="padding"> <!-- TODO1: Create the insert-image button. --> <!-- TODO3: Create the insert-text button. --> <!-- TODO4: Create the get-slide-metadata button. --> <!-- TODO5: Create the add-slides and go-to-slide buttons. --> </div> </div>
-
Open the file Home.js in the root of the web application project. This file specifies the script for the add-in. Replace the entire contents with the following code and save the file.
(function () { "use strict"; let messageBanner; Office.onReady(function () { $(document).ready(function () { // Initialize the FabricUI notification mechanism and hide it. const element = document.querySelector('.MessageBanner'); messageBanner = new components.MessageBanner(element); messageBanner.hideBanner(); // TODO1: Assign event handler for insert-image button. // TODO4: Assign event handler for insert-text button. // TODO6: Assign event handler for get-slide-metadata button. // TODO8: Assign event handlers for add-slides and the four navigation buttons. }); }); // TODO2: Define the insertImage function. // TODO3: Define the insertImageFromBase64String function. // TODO5: Define the insertText function. // TODO7: Define the getSlideMetadata function. // TODO9: Define the addSlides and navigation functions. // Helper function for displaying notifications. function showNotification(header, content) { $("#notification-header").text(header); $("#notification-body").text(content); messageBanner.showBanner(); messageBanner.toggleExpansion(); } })();
Complete the following steps to add code that retrieves the Bing photo of the day and inserts that image into a slide.
-
Using Solution Explorer, add a new folder named Controllers to the HelloWorldWeb project.
-
Right-click the Controllers folder and select Add > New Scaffolded Item....
-
In the Add Scaffold dialog window, select Web API 2 Controller - Empty and choose the Add button.
-
In the Add Controller dialog window, enter PhotoController as the controller name and choose the Add button. Visual Studio creates and opens the PhotoController.cs file.
[!IMPORTANT] The scaffolding process doesn't complete properly on some versions of Visual Studio after version 16.10.3. If you have the Global.asax and ./App_Start/WebApiConfig.cs files, then skip to step 6.
-
If you're missing scaffolding files from the HelloWorldWeb project, add them as follows.
-
Using Solution Explorer, add a new folder named App_Start to the HelloWorldWeb project.
-
Right-click the App_Start folder and select Add > Class....
-
In the Add New Item dialog, name the file WebApiConfig.cs then choose the Add button.
-
Replace the entire contents of the WebApiConfig.cs file with the following code.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; namespace HelloWorldWeb.App_Start { public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }
-
In the Solution Explorer, right-click the HelloWorldWeb project and select Add > New Item....
-
In the Add New Item dialog, search for "global", select Global Application Class, then choose the Add button. By default, the file is named Global.asax.
-
Replace the entire contents of the Global.asax.cs file with the following code.
using HelloWorldWeb.App_Start; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Security; using System.Web.SessionState; namespace HelloWorldWeb { public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); } } }
-
In the Solution Explorer, right-click the Global.asax file and choose View Markup.
-
Replace the entire contents of the Global.asax file with the following code.
<%@ Application Codebehind="Global.asax.cs" Inherits="HelloWorldWeb.WebApiApplication" Language="C#" %>
-
-
Replace the entire contents of the PhotoController.cs file with the following code that calls the Bing service to retrieve the photo of the day as a Base64-encoded string. When you use the Office JavaScript API to insert an image into a document, the image data must be specified as a Base64-encoded string.
using System; using System.IO; using System.Net; using System.Text; using System.Web.Http; using System.Xml; namespace HelloWorldWeb.Controllers { public class PhotoController : ApiController { public string Get() { string url = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1"; // Create the request. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { // Process the result. StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); string result = reader.ReadToEnd(); // Parse the XML response and get the URL. XmlDocument doc = new XmlDocument(); doc.LoadXml(result); string photoURL = "http://bing.com" + doc.SelectSingleNode("/images/image/url").InnerText; // Fetch the photo and return it as a Base64-encoded string. return getPhotoFromURL(photoURL); } } private string getPhotoFromURL(string imageURL) { var webClient = new WebClient(); byte[] imageBytes = webClient.DownloadData(imageURL); return Convert.ToBase64String(imageBytes); } } }
-
In the Home.html file, replace
TODO1
with the following markup. This markup defines the Insert Image button that will appear within the add-in's task pane.<button class="Button Button--primary" id="insert-image"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Insert Image</span> <span class="Button-description">Gets the photo of the day that shows on the Bing home page and adds it to the slide.</span> </button>
-
In the Home.js file, replace
TODO1
with the following code to assign the event handler for the Insert Image button.$('#insert-image').click(insertImage);
-
In the Home.js file, replace
TODO2
with the following code to define theinsertImage
function. This function fetches the image from the Bing web service and then calls theinsertImageFromBase64String
function to insert that image into the document.function insertImage() { // Get image from web service (as a Base64-encoded string). $.ajax({ url: "/api/photo/", dataType: "text", success: function (result) { insertImageFromBase64String(result); }, error: function (xhr, status, error) { showNotification("Error", "Oops, something went wrong."); } }); }
-
In the Home.js file, replace
TODO3
with the following code to define theinsertImageFromBase64String
function. This function uses the Office JavaScript API to insert the image into the document. Note:-
The
coercionType
option that's specified as the second parameter of thesetSelectedDataAsync
request indicates the type of data being inserted. -
The
asyncResult
object encapsulates the result of thesetSelectedDataAsync
request, including status and error information if the request failed.
function insertImageFromBase64String(image) { // Call Office.js to insert the image into the document. Office.context.document.setSelectedDataAsync(image, { coercionType: Office.CoercionType.Image }, function (asyncResult) { if (asyncResult.status === Office.AsyncResultStatus.Failed) { showNotification("Error", asyncResult.error.message); } }); }
-
-
Using Visual Studio, test the newly created PowerPoint add-in by pressing F5 or choosing the Start button to launch PowerPoint with the Show Taskpane add-in button displayed on the ribbon. The add-in will be hosted locally on IIS.
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
In the task pane, choose the Insert Image button to add the Bing photo of the day to the current slide.
[!NOTE] If you get an error "Could not find file [...]\bin\roslyn\csc.exe", then do the following:
- Open the .\Web.config file.
- Find the <compiler> node for the .cs
extension
, then remove thetype
attribute and its value. - Save the file.
-
In Visual Studio, stop the add-in by pressing Shift+F5 or choosing the Stop button. PowerPoint will automatically close when the add-in is stopped.
Complete the following steps to add markup that customizes the task pane UI.
-
In the Home.html file, replace
TODO2
with the following markup to add a header section and title to the task pane. Note:- The styles that begin with
ms-
are defined by Fabric Core in Office Add-ins, a JavaScript front-end framework for building user experiences for Office. The Home.html file includes a reference to the Fabric Core stylesheet.
<div id="content-header"> <div class="ms-Grid ms-bgColor-neutralPrimary"> <div class="ms-Grid-row"> <div class="padding ms-Grid-col ms-u-sm12 ms-u-md12 ms-u-lg12"> <div class="ms-font-xl ms-fontColor-white ms-fontWeight-semibold">My PowerPoint add-in</div></div> </div> </div> </div>
- The styles that begin with
-
In the Home.html file, find the div with
class="footer"
and delete that entire div to remove the footer section from the task pane.
-
Using Visual Studio, test the PowerPoint add-in by pressing F5 or choosing the Start button to launch PowerPoint with the Show Taskpane add-in button displayed on the ribbon. The add-in will be hosted locally on IIS.
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
Notice that the task pane now contains a header section and title, and no longer contains a footer section.
-
In Visual Studio, stop the add-in by pressing Shift+F5 or choosing the Stop button. PowerPoint will automatically close when the add-in is stopped.
Complete the following steps to add code that inserts text into the title slide which contains the Bing photo of the day.
-
In the Home.html file, replace
TODO3
with the following markup. This markup defines the Insert Text button that will appear within the add-in's task pane.<br /><br /> <button class="Button Button--primary" id="insert-text"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Insert Text</span> <span class="Button-description">Inserts text into the slide.</span> </button>
-
In the Home.js file, replace
TODO4
with the following code to assign the event handler for the Insert Text button.$('#insert-text').click(insertText);
-
In the Home.js file, replace
TODO5
with the following code to define theinsertText
function. This function inserts text into the current slide.function insertText() { Office.context.document.setSelectedDataAsync('Hello World!', function (asyncResult) { if (asyncResult.status === Office.AsyncResultStatus.Failed) { showNotification("Error", asyncResult.error.message); } }); }
-
Using Visual Studio, test the add-in by pressing F5 or choosing the Start button to launch PowerPoint with the Show Taskpane add-in button displayed on the ribbon. The add-in will be hosted locally on IIS.
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
In the task pane, choose the Insert Image button to add the Bing photo of the day to the current slide and choose a design for the slide that contains a text box for the title.
-
Put your cursor in the text box on the title slide and then in the task pane, choose the Insert Text button to add text to the slide.
-
In Visual Studio, stop the add-in by pressing Shift+F5 or choosing the Stop button. PowerPoint will automatically close when the add-in is stopped.
Complete the following steps to add code that retrieves metadata for the selected slide.
-
In the Home.html file, replace
TODO4
with the following markup. This markup defines the Get Slide Metadata button that will appear within the add-in's task pane.<br /><br /> <button class="Button Button--primary" id="get-slide-metadata"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Get Slide Metadata</span> <span class="Button-description">Gets metadata for the selected slide(s).</span> </button>
-
In the Home.js file, replace
TODO6
with the following code to assign the event handler for the Get Slide Metadata button.$('#get-slide-metadata').click(getSlideMetadata);
-
In the Home.js file, replace
TODO7
with the following code to define thegetSlideMetadata
function. This function retrieves metadata for the selected slide(s) and writes it to a popup dialog window within the add-in task pane.function getSlideMetadata() { Office.context.document.getSelectedDataAsync(Office.CoercionType.SlideRange, function (asyncResult) { if (asyncResult.status === Office.AsyncResultStatus.Failed) { showNotification("Error", asyncResult.error.message); } else { showNotification("Metadata for selected slide(s):", JSON.stringify(asyncResult.value), null, 2); } } ); }
-
Using Visual Studio, test the add-in by pressing F5 or choosing the Start button to launch PowerPoint with the Show Taskpane add-in button displayed on the ribbon. The add-in will be hosted locally on IIS.
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
In the task pane, choose the Get Slide Metadata button to get the metadata for the selected slide. The slide metadata is written to the popup dialog window at the bottom of the task pane. In this case, the
slides
array within the JSON metadata contains one object that specifies theid
,title
, andindex
of the selected slide. If multiple slides had been selected when you retrieved slide metadata, theslides
array within the JSON metadata would contain one object for each selected slide. -
In Visual Studio, stop the add-in by pressing Shift+F5 or choosing the Stop button. PowerPoint will automatically close when the add-in is stopped.
Complete the following steps to add code that navigates between the slides of a document.
-
In the Home.html file, replace
TODO5
with the following markup. This markup defines the four navigation buttons that will appear within the add-in's task pane.<br /><br /> <button class="Button Button--primary" id="add-slides"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Add Slides</span> <span class="Button-description">Adds 2 slides.</span> </button> <br /><br /> <button class="Button Button--primary" id="go-to-first-slide"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Go to First Slide</span> <span class="Button-description">Go to the first slide.</span> </button> <br /><br /> <button class="Button Button--primary" id="go-to-next-slide"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Go to Next Slide</span> <span class="Button-description">Go to the next slide.</span> </button> <br /><br /> <button class="Button Button--primary" id="go-to-previous-slide"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Go to Previous Slide</span> <span class="Button-description">Go to the previous slide.</span> </button> <br /><br /> <button class="Button Button--primary" id="go-to-last-slide"> <span class="Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span> <span class="Button-label">Go to Last Slide</span> <span class="Button-description">Go to the last slide.</span> </button>
-
In the Home.js file, replace
TODO8
with the following code to assign the event handlers for the Add Slides and four navigation buttons.$('#add-slides').click(addSlides); $('#go-to-first-slide').click(goToFirstSlide); $('#go-to-next-slide').click(goToNextSlide); $('#go-to-previous-slide').click(goToPreviousSlide); $('#go-to-last-slide').click(goToLastSlide);
-
In the Home.js file, replace
TODO9
with the following code to define theaddSlides
and navigation functions. Each of these functions uses thegoToByIdAsync
method to select a slide based upon its position in the document (first, last, previous, and next).async function addSlides() { await PowerPoint.run(async function (context) { context.presentation.slides.add(); context.presentation.slides.add(); await context.sync(); showNotification("Success", "Slides added."); goToLastSlide(); }); } function goToFirstSlide() { Office.context.document.goToByIdAsync(Office.Index.First, Office.GoToType.Index, function (asyncResult) { if (asyncResult.status == "failed") { showNotification("Error", asyncResult.error.message); } }); } function goToLastSlide() { Office.context.document.goToByIdAsync(Office.Index.Last, Office.GoToType.Index, function (asyncResult) { if (asyncResult.status == "failed") { showNotification("Error", asyncResult.error.message); } }); } function goToPreviousSlide() { Office.context.document.goToByIdAsync(Office.Index.Previous, Office.GoToType.Index, function (asyncResult) { if (asyncResult.status == "failed") { showNotification("Error", asyncResult.error.message); } }); } function goToNextSlide() { Office.context.document.goToByIdAsync(Office.Index.Next, Office.GoToType.Index, function (asyncResult) { if (asyncResult.status == "failed") { showNotification("Error", asyncResult.error.message); } }); }
-
Using Visual Studio, test the add-in by pressing F5 or choosing the Start button to launch PowerPoint with the Show Taskpane add-in button displayed on the ribbon. The add-in will be hosted locally on IIS.
-
If the add-in task pane isn't already open in PowerPoint, select the Show Taskpane button on the ribbon to open it.
-
In the task pane, choose the Add Slides button. Two new slides are added to the document and the last slide in the document is selected and displayed.
-
In the task pane, choose the Go to First Slide button. The first slide in the document is selected and displayed.
-
In the task pane, choose the Go to Next Slide button. The next slide in the document is selected and displayed.
-
In the task pane, choose the Go to Previous Slide button. The previous slide in the document is selected and displayed.
-
In the task pane, choose the Go to Last Slide button. The last slide in the document is selected and displayed.
-
In Visual Studio, stop the add-in by pressing Shift+F5 or choosing the Stop button. PowerPoint will automatically close when the add-in is stopped.
- Completed PowerPoint add-in tutorial: The result of completing this tutorial.
In this tutorial, you've created a PowerPoint add-in that inserts an image, inserts text, gets slide metadata, and navigates between slides. To learn more about building PowerPoint add-ins, continue to the following article.
[!div class="nextstepaction"] PowerPoint add-ins overview