Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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: ["firstname.name@email.com"],
         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 Reply

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: ["firstname.name@email.com"],
        subject: "Test Subject",
        htmlBody: "Internet headers: ",
        }, 
        { asyncContext : event }, 
        function (result) {
             console.log(result);
             event.completed();
        }
    );
}

It works! Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...