触发dialog.close后,Word加载项停止工作。 Office JS

时间:2017-03-22 11:43:31

标签: office365 office-js

我一直在开发一个Office365应用程序,在那里我打开一个对话框,当我用dialog.close()关闭对话框后进行某种活动。它工作得很好,但功能区按钮停止工作,下次它不会再显示相同的对话框。

  Office.context.ui.displayDialogAsync("https://" + location.host + "/Dialog.html", { width: 90, height: 90, requireHTTPS: true }, function (asyncResult) {
        dialog = asyncResult.value;
        dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
        if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
            return;
        }
    });

这是我的processMessage函数

function processMessage(arg) {
try{
    var messageFromDialog = JSON.parse(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.messageType === "dialogClosed") {
        dialog.close();
    }
} catch (ex) {
    console.log("Exception " + ex);
}
}

提前致谢:)

更新

此问题仅发生在网上办公室。

1 个答案:

答案 0 :(得分:0)

对调查此问题的延迟表示抱歉。长话短说,我不得不在你的代码中进行一些更改,这样可以工作,它抛出了几个例外(见下面的更改)。我没有你插入的图像所以我也假设你发送到该方法的base64是一个有效的图像。另外,请注意更新Office build 16.0.7967.2139已于4月21日发布,但这也适用于您引用的构建。

以下是我所做的更改:

  1. 我将此行更改为仅获取属性:var messageFromDialog = arg.message; (为什么要解析JSON?)
  2. 我也开始讨论这个:if(messageFromDialog ===“close”),我们没有messageType属性。(我们有类型) BTW我的Dialog.html页面看起来像这个(我看不到你的,但我假设用户正在选择一个图像)
  3. <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. 我还认为你没有检查对话框是否正确关闭,你是通过消息进行的,但你需要订阅DialogEventReceived事件以了解用户是否关闭了对话框(而不是消息)。
    2. 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);
          }
      }