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.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
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.
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