Can not insert text in bookmark in word document u

2019-07-10 22:56发布

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')

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)

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.

Here is my code-

        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);

1条回答
Lonely孤独者°
2楼-- · 2019-07-10 23:52

thanks so much for using our preview APIs. You just found a bug on it! I just repro this behavior. And yes the semantics for before, start, end, after are very clear on the API.

  1. "Start" and "End" insert locations imply that the insertions will be within the boundaries of the calling Range, on this case means that if you use any of those the bookmark needs to be expanded to whatever is inserted.
  2. On the other hand "Before"/"After" imply inserting outside the boundaries of the range.
  3. Finally, replace should replace the bookmark with whatever text supplied as parameter.

We'll get this fixed, although I don't have a clear timeline on this. thank you.

查看更多
登录 后发表回答