我已经完成了下一个功能。第一个使用“track”方法选择并存储内容控件:
export async function getContentControl () {
let cc = null
await window.Word.run(async context => {
try {
const recorder = context.document.getSelection()
recorder.load([
'parentContentControlOrNullObject',
'parentContentControlOrNullObject/isNullObject',
'parentContentControlOrNullObject/title'])
await context.sync()
cc = recorder.parentContentControlOrNullObject
console.log('cc', cc)
if (cc) {
console.log('pre track')
cc.load([
'font',
'font/highlightColor'
])
cc.track()
cc.font.highlightColor = null
await context.sync()
console.log('post track')
} else {
message.error('Here not exist any component! Please, select a component.')
}
} catch (error) {
console.log('error getInfoComponent', error)
}
})
return cc
}
第二个,它具有内容控件对象和一种颜色作为参数,使用该颜色来突出显示内容控件:
export async function highlightOneContentControl (currrentContentControl, color) {
await window.Word.run(async context => {
currrentContentControl.select()
console.log('currrentContentControl', currrentContentControl)
currrentContentControl.font.highlightColor = color
currrentContentControl.untrack()
await context.sync()
})
}
前端的代码是接下来的两个按钮:
<Button
onClick={async () => {
const cc = await getContentControl()
console.log('in button', cc)
setMyCC(cc)
}}
>
get CC
</Button>
<Button
onClick={async () => {
await highlightOneContentControl(myCC, 'Yellow')
}}
>
highlight CC
</Button>
console.log 消息是下一个: 控制台日志消息
如您所见,似乎 font.hightColor 已更改,但内容控件仍然没有突出显示: 内容控件的文字捕获