In my Excel add-in I want to perform multiple edits on my document sequentially. I'm using the promise chain to achive this.
Unfortunately I'm getting a GeneralException: An internal error has occurred.
from some of my edits.
The following example performs 250 edits and I get something between 20 and 30 GeneralExceptions in each run. (with Office 2016, in Office online it's much worse)
Example:
var promise;
Office.initialize = function (reason) {
// add awesome addin initialize code here
promise = new OfficeExtension.Promise(function (resolve, reject) { resolve(null); });
for (var i = 0; i < 200; i++) {
insertData("Data" + i);
}
}
function insertData(data) {
if (Office.context.requirements.isSetSupported("ExcelApi", "1.0")) {
//insert the data into the spreadsheet
promise = promise.then(function () {
Excel.run(function (ctx) {
var sheet = ctx.workbook.worksheets.getActiveWorksheet();
var range = ctx.workbook.getSelectedRange();
range.getCell(0, 0).values = data;
range.getCell(1, 0).select();
return ctx.sync()
}).catch(function (error) {
addLogEntry(error.message);
});
});
}
else if (Office.context.requirements.isSetSupported("WordApi", "1.0")) {
promise = promise.then(function () {
Word.run(function (ctx) {
var body = ctx.document.body;
var selectedRange = ctx.document.getSelection();
selectedRange.insertText(data + "\n", 'End');
selectedRange.select('End');
return ctx.sync();
}).catch(function (error) {
addLogEntry(error.message);
});
});
}
}
function addLogEntry(message) {
// log message here
}
What am I doing wrong?
Here the stack trace of the error:
"GeneralException: An internal error has occurred.
at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:9329:6)
at lib$es6$promise$$internal$$tryCatch (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11207:8)
at lib$es6$promise$$internal$$invokeCallback (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11217:8)
at lib$es6$promise$$internal$$publish (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11193:9)
at lib$es6$promise$asap$$flush (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11027:8)"