PBDoc
PBDoc

Reputation: 15

Inserting text following selection in Office-JS

I am trying to append a hyperlink immediately following a piece of selected text in Word 2013 using the Shared JS API.

Inserting the hyperlink using OOXML works fine when inserting at current cursor with no selection. My problem is 'finding' the end of the selected text to append the OOXML.

Just using setSelectedDataAsync overwrites the existing text. I have tried reading the selected text as OOXML and concatenating the hyperlink XML to it but without success.

I have not tried reading the current selection and then modifying that OOXML but would prefer to avoid.

In the Word JS API a before and after are provided on the selection so it is straightforward to do. Is it possible to do this in the Shared API? Thanks.

Upvotes: 1

Views: 1802

Answers (1)

Kim Brandl
Kim Brandl

Reputation: 13500

The following code sample illustrates the approach that Marc described in his comment above (with one exception: it gets the selected data as Text, not as HTML).

This snippet uses getSelectedDataAsync to get the selected data (as Text), and then appends a hyperlink to that data and uses setSelectedDataAsync to push that string back into the document (as HTML).

Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
{ valueFormat: "unformatted" },
function (asyncResult) {
    var error = asyncResult.error;
    if (asyncResult.status === Office.AsyncResultStatus.Failed) {
        console.log(error.name + ": " + error.message);
    }
    else {
        // Get selected data.
        var dataValue = asyncResult.value;
        console.log("Selected data is: " + dataValue);

        // Create newText by appending hyperlink to dataValue.
        var myHyperlink = "<a href='https://www.bing.com'>https://www.bing.com</a>";
        var newText = dataValue + " " + myHyperlink;
        console.log("New text is: " + newText);

        // Replace selected text with newText value.
        Office.context.document.setSelectedDataAsync(newText, { coercionType: "html" }, 
            function (asyncResult) {
                var error = asyncResult.error;
                if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                    console.log(error.name + ": " + error.message);
                }
            });
    }
});

Note: One side-effect of getting the selected data as Text, as this snippet does, is that when you write back that string (with the hyperlink appended) to the document, you'll lose any formatting (for example: font color, style, etc.) that was previously present in the selected text. If it's important that you preserve formatting of the originally selected text, you'll need to get the selected data as HTML and then append your hyperlink to the the portion of HTML which contains the originally selected text, before writing that HTML back to the document.

You can quickly and easily try this code snippet yourself in Word by using Script Lab (https://aka.ms/getscriptlab). Simply install the Script Lab add-in (free), then choose "Import" in the navigation menu, and use the following GIST URL: https://gist.github.com/kbrandl/8e235fb0ccc190bf42ed9ce1874f5559.

Upvotes: 1

Related Questions