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

使用Office.context.mailbox.item.dateTimeCreated返回undefined问题求助

解决Office.context.mailbox.item.dateTimeCreated返回undefined的问题

核心原因排查

  • dateTimeCreated 属性仅在邮件、会议请求、会议响应类项目中可用,若当前操作的是任务、联系人等非邮件类项目,该属性本身不存在,会直接返回undefined。
  • 该属性在Office.js 1.1及以上版本支持,但必须确保邮件项目已完全加载,直接在脚本初始化阶段调用会因item对象未就绪导致返回undefined。

分步解决步骤

  1. 在就绪回调中异步加载属性
    必须等待Office初始化完成且邮件项目加载就绪,通过loadAsync显式加载目标属性后再访问:

    Office.initialize = function(reason) {
        $(document).ready(function() {
            Office.context.mailbox.item.loadAsync(['dateTimeCreated'], function(result) {
                if (result.status === Office.AsyncResultStatus.Succeeded) {
                    console.log(Office.context.mailbox.item.dateTimeCreated);
                } else {
                    console.error('加载属性失败: ' + result.error.message);
                }
            });
        });
    };
    
  2. 验证当前项目类型兼容性
    先确认操作的项目类型是否支持该属性:

    const itemType = Office.context.mailbox.item.itemType;
    // 仅邮件、会议请求类项目支持dateTimeCreated
    if (itemType === Office.MailboxEnums.ItemType.Message || itemType === Office.MailboxEnums.ItemType.MeetingRequest) {
        // 执行属性获取逻辑
    } else {
        console.error('当前项目类型不支持dateTimeCreated属性');
    }
    
  3. 确认Office.js版本配置一致

    • 检查HTML中的script标签,确保引用的CDN版本与CLI切换的版本匹配,比如1.3版本的引用应为:
      <script src="https://appsforoffice.microsoft.com/lib/1.3/hosted/office.js" type="text/javascript"></script>
      
    • 清除浏览器缓存,避免旧版本Office.js缓存导致的版本不匹配问题。
  4. 检查权限配置
    在manifest.xml中确保已声明至少ReadItem权限:

    <Permissions>ReadItem</Permissions>
    

内容的提问来源于stack exchange,提问作者Rinku Gangishetty

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 14:11:12