如何使用 Office JS 在 Word 文档正文中添加书签 [英] How do I add a bookmark within the body of a Word doc using Office JS

查看:71
本文介绍了如何使用 Office JS 在 Word 文档正文中添加书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Office JS 在 预览,但我找不到任何示例.

The Office JS has provided the following function in preview, but I couldn't find any example.

这是我尝试过的但似乎不起作用,不知道我在这里缺少什么,因为此代码插入了文本但未创建书签.

Here is what I tried but it doesn't seem to work, any idea what I am missing here, since this code inserts the text but the bookmark is not created.

Word.run(function (context)
{
    let range = context.document.getSelection();
    return context.sync().then(function ()
    {
        range.insertText(`Test Bookmark`, Word.InsertLocation.replace);

        let uniqueStr = new Date().getTime();
        let bookmarkName = `Test_BookmarkCode_${uniqueStr}`;
        range.insertBookmark(bookmarkName);
    });
});

交叉发布 此处.

推荐答案

所以,这里是工作代码.显然,当我们插入文本时,返回了一个新的范围,我们需要使用该范围来插入书签.

So, here is the working code. Apparently, when we insertText, a new range is returned, we need to use that range to insertBookmark.

Word.run(function (context)
{
    let range = context.document.getSelection();
    return context.sync().then(function ()
    {
        let insertedTextRange = range.insertText(`Test Bookmark`, Word.InsertLocation.replace);

        let uniqueStr = new Date().getTime();
        let bookmarkName = `Test_BookmarkCode_${uniqueStr}`;
        insertedTextRange.insertBookmark(bookmarkName);
    });
});

这篇关于如何使用 Office JS 在 Word 文档正文中添加书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆