You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JavaScript函数提取Outlook邮件正文时返回undefined如何解决

解决方案

问题根因

Office.context.mailbox.item.body.getAsync 是异步执行的API,调用后不会立刻返回结果,你在回调函数外直接读取bodyContent时,回调还未执行,变量尚未被赋值,因此得到undefined。而标题可以直接读取是因为item.subject是同步可获取的属性,不需要异步调用。

修正后代码

document.getElementById("fill-data").onclick = function () {
    var item = Office.context.mailbox.item;
    // 同步给标题文本框赋值
    document.getElementById("txt-subject").value = item.subject;
    // 正文赋值逻辑必须放在异步回调内部执行
    Office.context.mailbox.item.body.getAsync('text', function (asyncResult) {
        // 可选:添加调用状态判断,处理异常场景
        if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
            const bodyContent = asyncResult.value;
            document.getElementById("txt-description").value = bodyContent;
        } else {
            document.getElementById("txt-description").value = "正文读取失败";
        }
    });
};

补充说明

  • 如果需要获取HTML格式的正文,把getAsync的第一个参数从'text'改为'html'即可
  • 所有Office JS中带Async后缀的API都是异步执行的,相关结果处理逻辑都要放到对应的回调函数内部,不能在调用外直接读取返回值

内容的提问来源于stack exchange,提问作者Mr.Curious

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 19:57:03