TypeScript


Excel.run ( async (context) => { 
   //code
   await context.sync();
}).catch(customErrorHandler};

try { 
await Excel.run ( async (context) => {
   //code
   await context.sync();
}
catch (e) {
    customErrorHandler(e);
}

Promises

$("#run").click(() => tryCatch(run)); 

function run() {
  return Word.run(function(context) {
    let _range = context.document.getSelection();
    _range.font.color = "red";
    _range.load("text");

    return context.sync().then(function() {
      console.log('The selected text was "' + _range.text + '".');
    });
  });
}

function tryCatch(callback) {
  Promise.resolve()
    .then(callback)
    .catch(function(error) {
      console.error(error);
    });
}

Async/Await

$("#run").click(() => tryCatch(run)); 

async function run() {
  await Word.run(async (context) => {
    let _range = context.document.getSelection();
    _range.font.color = "red";
    _range.load("text");

    await context.sync();
    console.log('The selected text was "' + _range.text + '".');
  });
}

async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    console.error(error);
  }
}


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext