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?
Add a comment
|
1 Answer
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
});
});
}
2 Comments
Ahmed Salah
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?Marc LaFleur
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.