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

如何在C#中获取Outlook保存确认对话框的结果

在C#中捕获Outlook保存确认对话框的结果

方法一:利用Outlook对象模型拦截事件

通过Outlook的Inspector对象BeforeClose事件,可在系统弹出默认对话框前介入处理,直接控制保存逻辑并获取结果,这是最稳定的方案。

代码示例

using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;

public class OutlookMailManager
{
    private Outlook.Inspector _activeInspector;
    private Outlook.MailItem _targetMail;

    public void HookToOpenMail()
    {
        var outlookApp = new Outlook.Application();
        _activeInspector = outlookApp.ActiveInspector();
        
        if (_activeInspector?.CurrentItem is Outlook.MailItem mailItem)
        {
            _targetMail = mailItem;
            _activeInspector.BeforeClose += OnInspectorBeforeClose;
        }
    }

    private void OnInspectorBeforeClose(ref bool Cancel)
    {
        // 判断邮件是否被修改
        if (!_targetMail.Saved)
        {
            // 自定义确认对话框,替代系统默认弹窗
            var result = MessageBox.Show("是否保存对邮件的修改?", "确认保存", MessageBoxButtons.YesNoCancel);
            
            switch (result)
            {
                case DialogResult.Yes:
                    _targetMail.Save();
                    Cancel = false; // 允许关闭窗口
                    break;
                case DialogResult.No:
                    _targetMail.Saved = true; // 标记为已保存,避免系统弹窗
                    Cancel = false;
                    break;
                case DialogResult.Cancel:
                    Cancel = true; // 取消关闭操作
                    break;
            }
            
            // 此处可记录result变量,即为你需要的确认结果
        }
    }
}

优势

  • 完全基于Outlook对象模型实现,不受UI版本变化影响
  • 可自定义交互逻辑,无需依赖系统默认对话框

方法二:通过Windows API捕获系统默认对话框

如果必须处理Outlook原生的保存确认对话框,可通过Windows API查找窗口并捕获用户操作,但这种方法依赖UI控件结构,不同Outlook版本可能失效。

代码示例

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;

public class OutlookDialogCapture
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    private const string DialogTitle = "Microsoft Outlook";
    private static DialogResult _dialogResult = DialogResult.None;

    public static DialogResult WaitForDialogResult()
    {
        _dialogResult = DialogResult.None;
        var thread = new Thread(MonitorDialog);
        thread.IsBackground = true;
        thread.Start();
        thread.Join();
        return _dialogResult;
    }

    private static void MonitorDialog()
    {
        while (_dialogResult == DialogResult.None)
        {
            IntPtr dialogHandle = FindWindow(null, DialogTitle);
            if (dialogHandle != IntPtr.Zero)
            {
                // 查找对话框按钮
                IntPtr yesBtn = FindWindowEx(dialogHandle, IntPtr.Zero, "Button", "&Yes");
                IntPtr noBtn = FindWindowEx(dialogHandle, IntPtr.Zero, "Button", "&No");
                IntPtr cancelBtn = FindWindowEx(dialogHandle, IntPtr.Zero, "Button", "&Cancel");

                // 可结合Windows钩子捕获按钮点击事件,此处为简化逻辑示例
                // 实际实现需补充钩子监听逻辑来判断用户选择
            }
            Thread.Sleep(100);
        }
    }
}

注意事项

  • 依赖Outlook的UI控件文本和结构,版本更新可能导致逻辑失效
  • 需要处理线程同步和窗口监听逻辑,实现复杂度较高

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 16:35:13