1

In the Word Web Add-in I can access font of the selected context.document.getSelection().font but I don't find it (after searching too) in Outlook Web Add-in, I only can get the text of selected by Office.context.mailbox.item.getSelectedDataAsync with Office.CoercionType.Text parameter, How can I get the font please?

1 Answer 1

3

Text formatting in Outlook is done in HTML (assuming the format isn't plain text). You can return the underlying HTML using Office.CoercionType.Html:

Office.initialize = function () {
    Office.context.mailbox.item
        .getSelectedDataAsync(Office.CoercionType.Html, {},
            function (asyncResult) {
                var htmlData = asyncResult.value.data;
                // do stuff
            });
}

Since the HTML formatting might have been set outside the scope of your selection, you might want to grab the entire body as well. You can then use the getSelectedDataAsync results to find the current selection within the full HTML body:

function myFunction() {

    // Get the selected text
    Office.context.mailbox.item
        .getSelectedDataAsync('html', {}, function (asyncResult) {

            // Get the full body and pass through the selectedData
            // in the asyncContext. 
            Office.context.mailbox.item.body.getAsync("html", {
                    asyncContext: asyncResult.value.data
                },
                function callback(asyncResult) {
                    // Get the body from the result
                    let bodyDaya = asyncResult.value.data;

                    // Get the selectedData we passed in
                    let selectedData = asyncResult.asyncContext;

                    // Do stuff
                });

        });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please excuse me for being beginner at office.js , I have used Office.CoercionType.Html instead and tried asyncResult.value.data.font.bold=true; , asyncResult.value.data.font.Bold=true; , asyncResult.value.data.bold=true; and asyncResult.value.data.Bold=true; but they all didn't work, Shall I do it in another way?
asyncResult.value.data is a string containing html markup, not an object. You'll need to parse the html to determine what style is being applied. Take a look at DOMParser as a potential option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.