WordJS API adding a hyperlink to a tablecell causes general exception

Bill King 45 Reputation points
2023-02-22T06:50:31.9666667+00:00

I am writing a Microsoft Word Add-in using the Word javascript API.

I am trying to create a new table and insert it into the document and then trying to add a hyperlink to one of the cells. This causes a GeneralException on the subsequent context.sync() call. A small example of this problem is:

export async function run() {
  return Word.run(async (context) => {
    const paragraph = context.document.body.insertParagraph("Hello World", Word.InsertLocation.end);
    await context.sync();
    const tableData: string[][] = [
      ["cell11", "cell12", "cell13"],
      ["cell21", "cell22", "cell23"],
      ["cell31", "cell32", "cell33"]
    ];
    const table = paragraph.insertTable(3, 3, Word.InsertLocation.after, tableData);
    await context.sync();
    // Tried loading contents of table nothing helped
    // table.load("rowCount, rows, rows/cells");
    // await context.sync();
    console.log("start adding link");
    const cell = table.getCell(1, 1);
    const range = cell.body.getRange();
    range.hyperlink = "https://www.google.com";
    try {
      await context.sync();
    } catch (err) {
      console.log("Unable to sync link addition: " + err.message);
    }
  });
}

The console shows:

start adding link
taskpane.ts:37 Unable to sync link addition: GeneralException

If I remove the range.hyperlink = ... line and replace it with:

  cell.body.font.color = "#800000";
  cell.shadingColor = "#cccccc";
  range.select();

Everything behaves as expected ie the font color and the shading of the cell changes and the cell text is selected in the Word UI, but the sync does not work if I add the hyperlink line. I assume I need to load something, but everything I have tried has not worked. Can anyone help?

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
714 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
912 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,647 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bill King 45 Reputation points
    2023-02-22T21:59:36.71+00:00

    Solved this by passing a parameter to the getRange function ie

    const range = cell.body.getRange(Word.RangeLocation.content)
    

    It is by design not allowed to add a hyperlink to a range of the whole cell.

    1 person found this answer helpful.
    0 comments No comments

  2. Bill King 45 Reputation points
    2023-02-22T22:02:30.86+00:00

    The getRange method accepts a Word.RangeLocation parameter with default value Word.RangeLocation.whole. When calling this method inside a tableCell with the default “whole” option, the table cell marker is also included and adding hyperlink on this range is forbidden (also the behavior on UI). So this is a by-design behavior, instead, please try to use the following way to just get the content range of the table cell. cell.body.getRange(Word.RangeLocation.content); Thanks to MSFT-jipyua via stackoverflow

    1 person found this answer helpful.
    0 comments No comments