I'm developing with JavaScript API for Office, MS Word 2016, VisualStudio 2015. There are multiple Rich Text ContentContols with a same title in the document. I'm trying to bind these ContentControls to a handler so that I can get onBindingDataChanged notification.
Is there a way to bind the ContentControls to one handler with their own ID? or pass ContentControls' id as one parameter?
My current code is like:
function bindNamedItem() {
Office.context.document.bindings.addFromNamedItemAsync("CCTitle", Office.BindingType.Text, { id: 'ccbind' }, function (result) {
if (result.status == 'succeeded') {
console.log('Added new binding with type: ' + result.value.type + ' and id: ' + result.value.id);
}
else
console.log('Error: ' + result.error.message);
});
}
function addEventHandlerToBinding() {
Office.select("bindings#ccbind").addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged);
}
var onBindingDataChanged = function (result) {
console.log(result);
}
Since there are multiple contentcontrols in the document with title "CCTitle", addFromNamedItemAsync
in function bindNamedItem
will give error: Multiple objects with the same name were found.
What I'm trying to achieve is to get the ContentControls' id and content whenever the user make some change to any of them. Is there any idea to help? Thanks in advance.
答案 0 :(得分:0)
正如您所发现的,内容控件的命名会阻止您根据名称进行绑定。但是,是可用于绑定到每个内容控件的变通方法:
此解决方法的一个缺点是存在许多链式异步调用,因此性能可能不如您所希望的那么快。因此,我建议将此操作绑定到某些用户操作和/或在任务窗格中添加加载UI,以避免混淆用户。
-Michael(PM for Office加载项)