Exercise - Insert and replace images, HTML, and tables

Completed

In this exercise, you'll add text inside and outside of selected ranges of text, and replace the text of a selected range. You'll also learn how to insert images, HTML, and tables into the document.

Important

This exercise assumes you have created the Word add-in in the previous exercise in this module.

Add text inside a range

  1. Open the file ./src/taskpane/taskpane.html.

  2. Locate the <button> element for the change-font button, and add the following markup after that line:

    <button class="ms-Button" id="insert-text-into-range">Insert Abbreviation</button><br/><br/>
    
  3. Open the file ./src/taskpane/taskpane.js.

  4. Within the Office.onReady() method call, locate the following line in the Office.onReady() method:

    document.getElementById("change-font").onclick = changeFont;
    

    Add the following code immediately after it:

    document.getElementById("insert-text-into-range").onclick = insertTextIntoRange;
    
  5. Add the following function to the end of the file:

    async function insertTextIntoRange() {
      await Word.run(async (context) => {
        // TODO1: Queue commands to insert text into a selected range.
    
        // TODO2: Load the text of the range and sync so that the
        //        current range text can be read.
    
        // TODO3: Queue commands to repeat the text of the original
        //        range at the end of the document.
    
        await context.sync();
      })
      .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
      });
    }
    
  6. Within the insertTextIntoRange() function, replace TODO1 with the following code:

    const doc = context.document;
    const originalRange = doc.getSelection();
    originalRange.insertText(" (C2R)", "End");
    

    Note

    • The method is intended to insert the abbreviation ["(C2R)"] into the end of the Range whose text is "Click-to-Run". It makes a simplifying assumption that the string is present and the user has selected it.
    • The first parameter of the Range.insertText method is the string to insert into the Range object.
    • The second parameter specifies where in the range the additional text should be inserted. Besides "End", the other possible options are "Start", "Before", "After", and "Replace".
    • The difference between "End" and "After" is that "End" inserts the new text inside the end of the existing range, but "After" creates a new range with the string and inserts the new range after the existing range. Similarly, "Start" inserts text inside the beginning of the existing range and "Before" inserts a new range. "Replace" replaces the text of the existing range with the string in the first parameter.
    • You saw in an earlier stage of the tutorial that the insert* methods of the body object don't have the "Before" and "After" options. This is because you can't put content outside of the document's body.
  7. We'll skip over TODO2 until the next section.

    Within the insertTextIntoRange() function, replace TODO3 with the following code. This code is similar to the code you created in the first stage of the tutorial, except that now you're inserting a new paragraph at the end of the document instead of at the start. This new paragraph will demonstrate that the new text is now part of the original range.

    doc.body.insertParagraph("Original range: " + originalRange.text, "End");
    

Add code to fetch document properties into the task pane's script objects

In all previous functions in this module, you queued commands to write to the Office document. Each function ended with a call to the context.sync() method, which sends the queued commands to the document to be executed.

But the code you added in the last step calls the originalRange.text property, and this is a significant difference from the earlier functions you wrote, because the originalRange object is only a proxy object that exists in your task pane's script. It doesn't know what the actual text of the range in the document is, so its text property can't have a real value.

It's necessary to first fetch the text value of the range from the document and use it to set the value of originalRange.text. Only then can originalRange.text be called without causing an exception to be thrown.

This fetching process has three steps:

  1. Queue a command to load (that is; fetch) the properties that your code needs to read.
  2. Call the context object's sync() method to send the queued command to the document for execution and return the requested information.
  3. Because the sync() method is asynchronous, ensure that it has completed before your code calls the properties that were fetched.

These steps must be completed whenever your code needs to read information from the Office document.

  1. Within the insertTextIntoRange() function, replace TODO2 with the following code.

    originalRange.load("text");
    await context.sync();
    
    // TODO4: Move the doc.body.insertParagraph line here.
    
    // TODO5: Move the final call of context.sync here and ensure
    //        that it doesn't run until the insertParagraph has
    //        been queued.
    
  2. Move the doc.body.insertParagraph line and paste in place of TODO4.

When you're done, the completed insertTextIntoRange() function should look like the following:

async function insertTextIntoRange() {
  await Word.run(async (context) => {
    const doc = context.document;
    const originalRange = doc.getSelection();
    originalRange.insertText(" (C2R)", "End");

    originalRange.load("text");
    await context.sync();

    doc.body.insertParagraph("Current text of original range: " + originalRange.text, "End");

    await context.sync();
  })
  .catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
      console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
  });
}

Add text between ranges

  1. Open the file ./src/taskpane/taskpane.html.

  2. Locate the <button> element for the insert-text-into-range button, and add the following markup after that line:

    <button class="ms-Button" id="insert-text-outside-range">Add Version Info</button><br/><br/>
    
  3. Open the file ./src/taskpane/taskpane.js.

  4. Within the Office.onReady() method call, locate the following line in the Office.onReady() method:

    document.getElementById("insert-text-into-range").onclick = insertTextIntoRange;
    

    Add the following code immediately after it:

    document.getElementById("insert-text-outside-range").onclick = insertTextBeforeRange;
    
  5. Add the following function to the end of the file:

    async function insertTextBeforeRange() {
      await Word.run(async (context) => {
        // TODO1: Queue commands to insert a new range before the
        //        selected range.
    
        // TODO2: Load the text of the original range and sync so that the
        //        range text can be read and inserted.
      })
      .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
      });
    }
    
  6. Within the insertTextBeforeRange() function, replace TODO1 with the following code:

    const doc = context.document;
    const originalRange = doc.getSelection();
    originalRange.insertText("Office 2019, ", "Before");
    

    Note

    • The method is intended to add a range whose text is "Office 2019, " before the range with text "Microsoft 365". It makes a simplifying assumption that the string is present and the user has selected it.
    • The first parameter of the Range.insertText() method is the string to add.
    • The second parameter specifies where in the range the additional text should be inserted. For more information about the location options, see the previous discussion of the insertTextIntoRange() function.
  7. Within the insertTextBeforeRange() function, replace TODO2 with the following code.

    originalRange.load("text");
    await context.sync();
    
    // TODO3: Queue commands to insert the original range as a
    //        paragraph at the end of the document.
    
    // TODO4: Make a final call of context.sync here and ensure
    //        that it doesn't run until the insertParagraph has
    //        been queued.
    
  8. Replace TODO3 with the following code. This new paragraph will demonstrate the fact that the new text isn't part of the original selected range. The original range still has only the text it had when it was selected.

    doc.body.insertParagraph("Current text of original range: " + originalRange.text, "End");
    
  9. Replace TODO4 with the following code:

    await context.sync();
    

Replace the text of a range

  1. Open the file ./src/taskpane/taskpane.html.

  2. Locate the <button> element for the insert-text-outside-range button, and add the following markup after that line:

    <button class="ms-Button" id="replace-text">Change Quantity Term</button><br/><br/>
    
  3. Open the file ./src/taskpane/taskpane.js.

  4. Within the Office.onReady() method call, locate the following line in the Office.onReady() method:

    document.getElementById("insert-text-outside-range").onclick = insertTextBeforeRange;
    

    Add the following code immediately after it:

    document.getElementById("replace-text").onclick = replaceText;
    
  5. Add the following function to the end of the file:

    async function replaceText() {
      await Word.run(async (context) => {
        // TODO1: Queue commands to replace the text.
    
        await context.sync();
      })
      .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
      });
    }
    
  6. Within the replaceText() function, replace TODO1 with the following code. The method is intended to replace the string "several" with the string "many". It makes a simplifying assumption that the string is present and the user has selected it.

    const doc = context.document;
    const originalRange = doc.getSelection();
    originalRange.insertText("many", "Replace");
    
  7. Verify that you've saved all of the changes you've made to the project.

Test the add-in

  1. Repeat the steps from the previous exercise to sideload the add-in.
  2. If the add-in task pane isn't already open in Word, go to the Home tab, and select the Show Taskpane button in the ribbon to open it.
  3. In the task pane, select the Insert Paragraph button to ensure that there's a paragraph at the start of the document.
  4. Within the document, select the phrase "Click-to-Run". Be careful not to include the preceding space or following comma in the selection.
  5. Select the Insert Abbreviation button. Note that " (C2R)" is added. Note also that at the bottom of the document a new paragraph is added with the entire expanded text because the new string was added to the existing range.
  6. Within the document, select the phrase "Microsoft 365". Be careful not to include the preceding or following space in the selection.
  7. Select the Add Version Info button. Note that "Office 2019, " is inserted between "Office 2016" and "Microsoft 365". Note also that at the bottom of the document a new paragraph is added but it contains only the originally selected text because the new string became a new range rather than being added to the original range.
  8. Within the document, select the word "several". Be careful not to include the preceding or following space in the selection.
  9. Select the Change Quantity Term button. Note that "many" replaces the selected text.

Screenshot of text added and replaced by tutorial in Word.

Insert images, HTML, and tables

In this step of the tutorial, you'll learn how to insert images, HTML, and tables into the document.

Define an image

Complete the following steps to define the image that you'll insert into the document in the next part of this tutorial.

  1. In the root of the project, create a new file named ./src/taskpane/base64Image.js.

  2. Open the file base64Image.js and add the following code to specify the base64-encoded string that represents an image.

    export const base64Image =
        "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEFCAIAAABCdiZrAAAACXBIWXMAAAsSAA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R4L8Lexi5/WEIdWEgkUAIJFGvoKbTS+jlYlPVm9h5zU2TUYWKFhketnaeY3MLi9GRFL1yZfYqlOqKFjEK8kcNk1sv+qHoUgoFzmLzSfYqjOyQMEiQZAysFXHJ19OMWaZuCpjV3D9EXbYv5iCRQJnrYBti9uIgUmVvYzBIcUAAAIqSURBVAmYLfNiULBIaGRK2GlyG9HfNdzFtsVNQAoWiYrBNiJlayq4CUjBIjMyNWnkK9i2uI3oVqq4CUjBIjPG3kbcec1tRPUlysL4nJuAFCwSJ9mytxEpWyNF6Ao2n2CnqZyXQShYZGasFbBV5zZiX6rsTUDmFShYJNbY24jXHy3venxmt39omZuAFCwyH2TLy7iNuH6nvwlIqaJgkXmzRcu0jWhvAho1bgJSsMg8M9hGXL+zoD9gtp9X4CYgBYssjmwZtUXbRrQPLe80KVUULLKI2NuIxudzv41obwJuW9wEpGCRRWe92O/FPKfr8VfucROQgkWWjExp/rYR7c7FG1VKFQWLLB+DXszx30a0NwF5aJlQsChb/W3EeMpW6gY3AQkFi4xipx9itY1obwJuW5QqIj5keQkIEJuRrhxfSlhhkSlka4YjXTm+lFCwyNREP9KV40sJBYv4sGY/bCNeuRfuC63ewvYrbgISChYJQrY2qmFtIw46F6cMXmlCwSIBEfhIV44vJRQsEi6BjHTl+FJCwSLR4XmkK8eXEgoWmQ3TjnTl+FJCwSIzZjDSVQPHl5JAee/du3e8CsQX3Sa6Y730pB8khIJFCKElJIQQChYhhFCwCCEULEIIoWARQggFixBCwSKEEAoWIYRQsAghFCxCCKFgEUIIBYsQQsEihBAKFiGEULAIIRQsQgihYBFCCAWLEELBIoQQChYhhILFS0AIoWARQkjA/D87uqZQTj7xTgAAAABJRU5ErkJggg==";
    

Insert an image

  1. Open the file ./src/taskpane/taskpane.html.

  2. Locate the <button> element for the replace-text button, and add the following markup after that line:

    <button class="ms-Button" id="insert-image">Insert Image</button><br/><br/>
    
  3. Open the file ./src/taskpane/taskpane.js.

  4. Locate the Office.onReady() method call near the top of the file and add the following code immediately before that line. This code imports the variable that you defined previously in the file base64Image.js.

    import { base64Image } from "./base64Image";
    
  5. Within the Office.onReady() method call, locate the following line in the Office.onReady() method:

    document.getElementById("replace-text").onclick = replaceText;
    

    Add the following code immediately after it:

    document.getElementById("insert-image").onclick = insertImage;
    
  6. Add the following function to the end of the file:

    async function insertImage() {
      await Word.run(async (context) => {
        // TODO1: Queue commands to insert an image.
    
        await context.sync();
      })
      .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
      });
    }
    
  7. Within the insertImage() function, replace TODO1 with the following code. This line inserts the base 64 encoded image at the end of the document. (The Paragraph object also has an insertInlinePictureFromBase64() method and other insert* methods. See the following insertHTML section for an example.)

    context.document.body.insertInlinePictureFromBase64(base64Image, "End");
    

Insert HTML

  1. Open the file ./src/taskpane/taskpane.html.

  2. Locate the <button> element for the insert-image button, and add the following markup after that line:

    <button class="ms-Button" id="insert-html">Insert HTML</button><br/><br/>
    
  3. Open the file ./src/taskpane/taskpane.js.

  4. Within the Office.onReady() method call, locate the following line in the Office.onReady() method:

    document.getElementById("insert-image").onclick = insertImage;
    

    Add the following code immediately after it:

    document.getElementById("insert-html").onclick = insertHTML;
    
  5. Add the following function to the end of the file:

    async function insertHTML() {
      await Word.run(async (context) => {
        // TODO1: Queue commands to insert a string of HTML.
    
        await context.sync();
      })
      .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
      });
    }
    
  6. Within the insertHTML() function, replace TODO1 with the following code:

    const blankParagraph = context.document.body.paragraphs.getLast().insertParagraph("", "After");
    blankParagraph.insertHtml('<p style="font-family: verdana;">Inserted HTML.</p><p>Another paragraph</p>', "End");
    

    Note

    • The first line adds a blank paragraph to the end of the document.
    • The second line inserts a string of HTML at the end of the paragraph; specifically two paragraphs, one formatted with Verdana font, the other with the default styling of the Word document. (As you saw in the insertImage() method earlier, the context.document.body object also has the insert* methods.)

Insert a table

  1. Open the file ./src/taskpane/taskpane.html.

  2. Locate the <button> element for the insert-html button, and add the following markup after that line:

    <button class="ms-Button" id="insert-table">Insert Table</button><br/><br/>
    
  3. Open the file ./src/taskpane/taskpane.js.

  4. Within the Office.onReady() method call, locate the following line in the Office.onReady() method:

    document.getElementById("insert-html").onclick = insertHTML;
    

    Add the following code immediately after it:

    document.getElementById("insert-table").onclick = insertTable;
    
  5. Add the following function to the end of the file:

    async function insertTable() {
      await Word.run(async (context) => {
        // TODO1: Queue commands to get a reference to the paragraph
        //        that will proceed the table.
    
        // TODO2: Queue commands to create a table and populate it with data.
    
        await context.sync();
      })
      .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
      });
    }
    
  6. Within the insertTable() function, replace TODO1 with the following code. This line uses the ParagraphCollection.getFirst() method to get a reference to the first paragraph and then uses the Paragraph.getNext() method to get a reference to the second paragraph.

    const secondParagraph = context.document.body.paragraphs.getFirst().getNext();
    
  7. Within the insertTable() function, replace TODO2 with the following code:

    const tableData = [
        ["Name", "ID", "Birth City"],
        ["Bob", "434", "Chicago"],
        ["Sue", "719", "Havana"],
    ];
    secondParagraph.insertTable(3, 3, "After", tableData);
    

    Note

    • The first two parameters of the insertTable() method specify the number of rows and columns.
    • The third parameter specifies where to insert the table, in this case after the paragraph.
    • The fourth parameter is a two-dimensional array that sets the values of the table cells.
    • The table will have plain default styling, but the insertTable() method returns a Table object with many members, some of which are used to style the table.
  8. Verify that you've saved all of the changes you've made to the project.

Test the add-in

  1. Repeat the steps from the previous exercise to sideload the add-in.
  2. If the add-in task pane isn't already open in Word, go to the Home tab, and select the Show Taskpane button in the ribbon to open it.
  3. In the task pane, select the Insert Paragraph button at least three times to ensure that there are a few paragraphs in the document.
  4. Select the Insert Image button and note that an image is inserted at the end of the document.
  5. Select the Insert HTML button and note that two paragraphs are inserted at the end of the document, and that the first one has Verdana font.
  6. Select the Insert Table button and note that a table is inserted after the second paragraph.

Screenshot of an image, HTML, and a table inserted by tutorial in Word.

Summary

In this exercise, you updated the add-in you created in a previous exercise to add images, HTML, and tables to a Word document. You also learned how to insert images, HTML, and tables into the document.

Test your knowledge

1.

Which of the following options will return the selected range of text in the current document?

2.

Which of the following options will insert a new image from a base 64 string at the end of a Word document?