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

如何通过C#开发的Add-in Express/VSTO插件设置Outlook离线模式?

实现Outlook插件编程切换离线模式

不管使用Add-in Express还是VSTO开发Outlook插件,都可以通过以下两种方案实现编程切换离线模式:

方案一:模拟点击内置"脱机工作"按钮(简单快捷)

该方案通过定位Outlook标准命令栏中的"脱机工作"按钮并执行点击操作,无需额外依赖,但依赖Outlook默认界面布局。

C#代码示例

using Outlook = Microsoft.Office.Interop.Outlook;

public void ToggleOfflineMode()
{
    // 获取Outlook应用实例(Add-in Express用AddinModule.CurrentInstance.OutlookApp)
    Outlook.Application outlookApp = Globals.ThisAddIn.Application;
    Outlook.CommandBarControl offlineButton = null;

    // 遍历标准命令栏,定位脱机工作按钮(命令ID为709)
    foreach (Outlook.CommandBar bar in outlookApp.ActiveExplorer().CommandBars)
    {
        if (bar.Name == "Standard")
        {
            offlineButton = bar.FindControl(Outlook.MsoControlType.msoControlButton, 709);
            break;
        }
    }

    offlineButton?.Execute();
}

方案二:调用MAPI API直接设置(更可靠)

通过P/Invoke调用MAPI底层接口直接控制连接状态,不受界面布局影响,稳定性更高。

C#代码示例

using System;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;

public class OutlookOfflineManager
{
    [DllImport("mapi32.dll", CharSet = CharSet.Unicode)]
    private static extern int MAPILogonEx(
        uint uiParam,
        string lpszProfileName,
        string lpszPassword,
        uint flFlags,
        uint ulReserved,
        out IntPtr lppSession);

    [DllImport("mapi32.dll")]
    private static extern int MAPISetStatus(
        IntPtr lpSession,
        uint ulFlags);

    [DllImport("mapi32.dll")]
    private static extern int MAPILogoff(
        IntPtr lpSession,
        IntPtr uiParam,
        uint ulFlags,
        uint ulReserved);

    private const uint MAPI_NEW_SESSION = 0x00000002;
    private const uint MAPI_NO_MAIL = 0x00000008;
    private const uint MAPI_OFFLINE = 0x00000020;
    private const uint MAPI_ONLINE = 0x00000040;

    public void SetOfflineStatus(bool enableOffline)
    {
        IntPtr mapiSession = IntPtr.Zero;
        try
        {
            // 登录到当前Outlook配置文件
            int hr = MAPILogonEx(0, null, null, MAPI_NEW_SESSION | MAPI_NO_MAIL, 0, out mapiSession);
            if (hr != 0)
                throw new System.Runtime.InteropServices.COMException("MAPI登录失败", hr);

            // 设置离线/在线状态
            uint statusFlag = enableOffline ? MAPI_OFFLINE : MAPI_ONLINE;
            hr = MAPISetStatus(mapiSession, statusFlag);
            if (hr != 0)
                throw new System.Runtime.InteropServices.COMException("设置连接状态失败", hr);
        }
        finally
        {
            // 确保注销MAPI会话
            if (mapiSession != IntPtr.Zero)
                MAPILogoff(mapiSession, IntPtr.Zero, 0, 0);
        }
    }
}

使用方式

在插件中调用即可:

var offlineManager = new OutlookOfflineManager();
offlineManager.SetOfflineStatus(true); // 切换到离线模式
// offlineManager.SetOfflineStatus(false); // 切换到在线模式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 08:06:24