Ahmed Salah
Ahmed Salah

Reputation: 969

How can I access the font of selected, Outlook Web Add-in JavaScript?

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?

Upvotes: 1

Views: 295

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33124

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

        });
}

Upvotes: 3

Related Questions