Word add-in stops working after dialog.close is fired. Office JS

别说谁变了你拦得住时间么 提交于 2019-12-01 20:54:13

Sorry about the delay investigating this issue. Long story short, I had to do a couple of changes in your code so this can work, it was throwing a couple of exceptions (see changes below). I did not had the images you are inserting so I am also assuming the base64 you are sending to the method is a valid image. Also FYI please update Office build 16.0.7967.2139 was shipped Apr 21st, but this should also work on the build you cited.

Here are the changes I made:

  1. I changed this line to only get the property: var messageFromDialog = arg.message; (why are you parsing JSON?)
  2. I also chagned this one : if (messageFromDialog === "close"), we dont have a messageType property.(we have type) BTW my Dialog.html page looks like this one ( I cannot see yours but i am assuming the user is selecting an image on it)

<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.1.min.js"></script>

    <!-- For the Office UI Fabric, go to http://aka.ms/office-ui-fabric to learn more. -->
    <link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.min.css">
    <link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.components.min.css">
    <script>
        Office.initialize = function () {
            $('#button1').click(one);
            $('#button2').click(two);
        };

        function one()
        {
            Office.context.ui.messageParent("Picked 1");
        }

        function two() {
            Office.context.ui.messageParent("close");
        }

    </script>
</head>
<body>
    <p class="ms-font-xxl ms-fontColor-neutralSecondary ms-fontWeight-semilight">Pick a number</p>
    <button class="ms-Button ms-Button--primary" id="button1">
        <span class="ms-Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span>
        <span class="ms-Button-label" id="button1-text">1</span>
        <span class="ms-Button-description" id="button1-desc">Number 1</span>
    </button>
    <button class="ms-Button ms-Button--primary" id="button2">
        <span class="ms-Button-icon"><i class="ms-Icon ms-Icon--plus"></i></span>
        <span class="ms-Button-label" id="button2-text">2</span>
        <span class="ms-Button-description" id="button2-desc">Number 2</span>
    </button>
</body>
</html>
  1. I also think you are not checking if the dialog was closed properly, you are doing it through the message, but you need so subscribe to the DialogEventReceived event to know if the user closed the dialog (and not with a message).

function showDialog() {


    var url = "https://" + location.host + "/Dialog.html";
    Office.context.ui.displayDialogAsync(url, { width: 10, height: 10, requireHTTPS: true }, function (asyncResult) {
        dialog = asyncResult.value;
        dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
        if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
            app.showDialog("OK");
            return;
        }
        else {
            app.showDialog("whats up");
        }
    });

}


function processMessage(arg) {
    try {
        var messageFromDialog = arg.message;
       
        var base64 = messageFromDialog.image.split(",")[1];
        Word.run(function (context) {
var body = context.document.getSelection();
            body.insertInlinePictureFromBase64(base64, Word.InsertLocation.replace);
            return context.sync();
        }).catch(function (error) {
            app.showNotification("Error: " + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                app.showNotification("Debug info: " + JSON.stringify(error.debugInfo));
            }
        });
        if (messageFromDialog === "close") {
            dialog.close();
        }
    } catch (ex) {
        console.log("Exception " + ex);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!