Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

office js - Callback functions not executed correctly - Outlook add-in

I'm trying to create an Outlook add-in for testing purposes but I'm stuck with a weird behaviour. I declared, in my XML Manifest the following keys:

<FunctionFile resid="functionFile" />

<Action xsi:type="ExecuteFunction">
    <FunctionName>myEntryPoint</FunctionName>
</Action>

The 'functionFile' is my HTML file that contains the Javascript code. In that HTML file, I've the following code:

(function () {
    Office.initialize = function (reason) { //Nothing here };
})();

function myEntryPoint(event) {

    Office.context.mailbox.displayNewMessageFormAsync({
         toRecipients: ["[email protected]"],
         subject: "Test Subject",
         htmlBody: "Internet headers: ",
    }, function (result) {
         console.log(result);
    });
    event.completed();
}

In fact, the method displayNewMessageFormAsync is executed but not the callback (console.log(result)).

If I put that code in the 'Office.initialize' function, the callback function is executed.

Any idea?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Regarding the comment of Outlook Add-ins Team - MSFT:

you are calling event.completed() after calling displayNewMessageFormAsync(). Which means the callback is not guaranteed to run. You can try setting a global variable to event, and calling event.completed() in the callback. or pass the event via the asyncContext.

I modified my code to pass the event in the asyncContext:

function myEntryPoint(event) {
    Office.context.mailbox.displayNewMessageFormAsync({
        toRecipients: ["[email protected]"],
        subject: "Test Subject",
        htmlBody: "Internet headers: ",
        }, 
        { asyncContext : event }, 
        function (result) {
             console.log(result);
             event.completed();
        }
    );
}

It works! Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...