如何通过Outlook Add-in在附件菜单中添加按钮?
在Outlook Add-in的附件菜单中添加自定义按钮的实现方法
要在选中附件后的上下文菜单中添加自定义按钮,核心是通过Office Add-in的清单文件配置对应的扩展点,以下是具体步骤:
1. 在清单文件(manifest.xml)中配置附件菜单扩展点
使用MessageAttachmentCommandSurface扩展点,这是Office.js专门为附件上下文菜单提供的扩展入口,配置后会自动在OWA和桌面端Outlook的附件菜单中显示你的按钮。
在清单的<VersionOverrides>节点下添加如下配置:
<VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0"> <Requirements> <bt:Sets DefaultMinVersion="1.3"> <bt:Set Name="Mailbox" /> </bt:Sets> </Requirements> <Hosts> <Host xsi:type="MailHost"> <!-- 桌面端配置 --> <DesktopFormFactor> <ExtensionPoint xsi:type="MessageAttachmentCommandSurface"> <OfficeTab id="TabDefault"> <Group id="MyAttachmentToolGroup"> <Label resid="AttachmentGroupLabel" /> <Control xsi:type="Button" id="MyAttachmentActionButton"> <Label resid="AttachmentButtonLabel" /> <Supertip> <Title resid="AttachmentButtonTitle" /> <Description resid="AttachmentButtonDesc" /> </Supertip> <Icon> <bt:Image size="16" resid="Icon16" /> <bt:Image size="32" resid="Icon32" /> <bt:Image size="80" resid="Icon80" /> </Icon> <Action xsi:type="ExecuteFunction"> <FunctionName>handleSelectedAttachment</FunctionName> </Action> </Control> </Group> </OfficeTab> </ExtensionPoint> </DesktopFormFactor> <!-- OWA/移动端配置(可选,桌面端配置已覆盖大部分场景) --> <MobileFormFactor> <ExtensionPoint xsi:type="MessageAttachmentCommandSurface"> <Group id="MyMobileAttachmentGroup"> <Label resid="AttachmentGroupLabel" /> <Control xsi:type="Button" id="MyMobileAttachmentButton"> <Label resid="AttachmentButtonLabel" /> <Icon> <bt:Image size="25" resid="Icon25" /> <bt:Image size="32" resid="Icon32" /> <bt:Image size="48" resid="Icon48" /> </Icon> <Action xsi:type="ExecuteFunction"> <FunctionName>handleSelectedAttachment</FunctionName> </Action> </Control> </Group> </ExtensionPoint> </MobileFormFactor> </Host> </Hosts> <!-- 资源配置:图标、文本 --> <Resources> <bt:Images> <!-- 替换为你的实际图标路径 --> <bt:Image id="Icon16" DefaultValue="https://localhost:3000/assets/icon-16.png" /> <bt:Image id="Icon25" DefaultValue="https://localhost:3000/assets/icon-25.png" /> <bt:Image id="Icon32" DefaultValue="https://localhost:3000/assets/icon-32.png" /> <bt:Image id="Icon48" DefaultValue="https://localhost:3000/assets/icon-48.png" /> <bt:Image id="Icon80" DefaultValue="https://localhost:3000/assets/icon-80.png" /> </bt:Images> <bt:ShortStrings> <bt:String id="AttachmentGroupLabel" DefaultValue="我的附件工具" /> <bt:String id="AttachmentButtonLabel" DefaultValue="处理附件" /> <bt:String id="AttachmentButtonTitle" DefaultValue="处理选中的附件" /> </bt:ShortStrings> <bt:LongStrings> <bt:String id="AttachmentButtonDesc" DefaultValue="对当前选中的附件执行自定义处理" /> </bt:LongStrings> </Resources> </VersionOverrides>
2. 实现按钮点击的处理函数
在你的Office.js业务代码中,实现清单中指定的handleSelectedAttachment函数,获取选中的附件并执行你已有的功能逻辑:
function handleSelectedAttachment(event) { // 获取当前邮件的所有附件 Office.context.mailbox.item.getAttachmentsAsync((result) => { if (result.status === Office.AsyncResultStatus.Succeeded) { // 筛选出用户选中的附件 const selectedAttachment = result.value.find(att => att.isSelected); if (selectedAttachment) { // 这里调用你已在任务窗格中实现的处理逻辑 console.log('选中的附件信息:', selectedAttachment); // 示例:如果需要下载附件内容,可调用getAttachmentContentAsync Office.context.mailbox.item.getAttachmentContentAsync(selectedAttachment.id, (contentResult) => { if (contentResult.status === Office.AsyncResultStatus.Succeeded) { // 处理附件内容 console.log('附件内容:', contentResult.value); } }); } } else { console.error('获取附件失败:', result.error.message); } // 必须调用event.completed()通知Office操作完成 event.completed(); }); } // 注册命令函数,确保Office.js加载完成后执行 Office.onReady(() => { Office.actions.associate("handleSelectedAttachment", handleSelectedAttachment); });
3. 关键注意事项
- 版本兼容:要求Outlook客户端支持Mailbox API 1.3及以上,清单中已通过
DefaultMinVersion="1.3"声明。 - 权限配置:如果需要访问附件内容,需在清单的
<Permissions>节点设置对应权限,比如ReadWriteMailbox或ReadItem。 - 测试验证:部署加载项后,在OWA和桌面端Outlook中选中附件,检查菜单是否显示自定义按钮,测试功能是否正常执行。
内容的提问来源于stack exchange,提问作者Phil Salomon
相关产品推荐
相关产品推荐

