使用Office.js进行Office加载项开发时无法在Word文档的书签中插入文本 [英] Can not insert text in bookmark in word document using office.js for office add-in development

查看:104
本文介绍了使用Office.js进行Office加载项开发时无法在Word文档的书签中插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个单词加载项.主要任务是使用数据库中的数据填充文档中的书签.在office.js中,我发现可以将书签作为范围对象进行访问,而我我正在使用"rangeObject.insertText(text,insertLocation)"方法将数据插入书签.但是问题是第二个参数insertLocation的值可以是"Replace","Start","End","Before"或"After".基本上是将数据附加到书签之前或之后.像这样- 图像1(使用insertLocation值"End"的当前结果)

I am developing an add-in for word.The main task is to populate the bookmarks in a document with data from the database.In office.js all i have found that a bookmark can be accessed as a range object and i am using "rangeObject.insertText(text, insertLocation)" method for inserting data into a bookmark.But the problem is 2nd parameter insertLocation value can be 'Replace', 'Start', 'End', 'Before' or 'After'.Which basically appends the data before or after the bookmark.like this - image 1 (current result using insertLocation value 'End')

使用insertLocation值替换",它将替换整个书签,并实际上从文档中删除书签!因此,我只想在书签中插入一个文本,如下所示: 图片2(所需结果)

using insertLocation value 'Replace' it replaces the whole bookmark and actually deletes the bookmark from the document!.So all i want is insert a text in the bookmark like this- image 2 (desired result)

注意:我必须稍后阅读这些书签值.因此不能删除任何书签.我正在使用1.4 beta版本的office.js api.

Note: I have to read these bookmark value later.So no bookmark can be deleted.and i am using 1.4 beta version of the office.js api for purpose.

这是我的代码-

        Word.run(function (context) {

            var doc = context.document;

            //get the bookmark range by its name
            var bookmarkRange = doc.getBookmarkRangeOrNullObject("cscasenumber01");

            //insert a data
            bookmarkRange.insertText("test data",'end');


            // Synchronize the document state by executing the queued commands, 
            return context.sync();

        }).catch(errorHandler);

推荐答案

查看此问题后,我们发现替换"行为是设计使然.

After reviewing this issue we just found that the "Replace" behavior is by design.

实际上,如果直接使用UI替换包含书签的范围的文本,则书签将被删除.如果您使用Macro/VBA以编程方式执行此操作,则同样如此.

In fact if you use directly the UI to replace the text of a range containing the bookmark, the bookmark will be deleted. The same is true if you do it programmatically with a Macro/VBA.

好消息是,替换文本后,所选内容将是替换的文本范围,因此,如果需要,您可以在替换操作之后重新创建书签.

The good news is that after replacing the text, the selection will be the replaced text range, so you could re-create the bookmark after the replace operation, if so needed.

请注意,这仅在替换时发生.在开始"或结束"处插入将保留书签.

Note that this only happens with replace. Inserting on Start or End will keep the bookmark.

最后,书签API仍处于预览状态,发布日期未知.

Finally the Bookmark API is still in preview and has an unknown release date.

---------保留预览的某些上下文答案---------

--------- Keeping some context of the previews answer ---------

非常感谢您使用我们的预览API.我只是复制这种行为,并且是设计使然.是的,在API上,before,start,end和after的语义非常清楚.

thanks so much for using our preview APIs. I just repro this behavior, and its by design. And yes the semantics for before, start, end, after are very clear on the API.

  1. 开始"和结束"插入位置表示插入将在调用范围的边界内,在这种情况下,这意味着如果您使用其中的任何一个,则书签都需要扩展到插入的任何位置.
  2. li>
  3. 另一方面,之前"/之后"表示要插入范围的边界之外.
  4. 最后,replace应该用作为参数提供的任何文本替换书签,书签将被删除(与直接在UI中和通过VBA编程地进行编辑保持一致).

谢谢

这篇关于使用Office.js进行Office加载项开发时无法在Word文档的书签中插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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