VSTO Outlook:如何判断打开的窗口是否为撰写窗口
判断当前窗口是否为邮件撰写窗口的实现方案
网页版邮箱(浏览器环境)
- 匹配窗口标题:绝大多数网页邮箱的撰写窗口标题包含固定关键词(如「撰写」「新建邮件」),可通过读取页面标题判断:
// 通用判断逻辑,可根据目标邮箱调整关键词 const isComposeWindow = document.title.includes('撰写') || document.title.includes('新建邮件'); - 识别DOM特征:撰写窗口会有独特的页面元素,比如Gmail的撰写区域带有
compose类,Outlook网页版的正文框带有aria-label="邮件正文"属性,通过查询这些元素验证:// Gmail 示例 const isGmailCompose = document.querySelector('.compose [role="textbox"]') !== null; // Outlook 网页版示例 const isOutlookCompose = document.querySelector('[aria-label="邮件正文"]') !== null;
桌面邮件客户端(以Outlook为例,VBA脚本)
通过窗口类型和邮件对象属性判断:撰写窗口的WindowType为olInspector,且关联的MailItem初始状态下主题为空或默认「未命名」:
Function IsComposeWindow() As Boolean Dim currWindow As Outlook.Window Set currWindow = Application.ActiveWindow If currWindow.Type = olInspector Then Dim currItem As Object Set currItem = currWindow.CurrentItem If TypeName(currItem) = "MailItem" Then ' 匹配新建邮件的初始主题特征 IsComposeWindow = (currItem.Subject = "" Or currItem.Subject = "未命名") End If End If End Function
自动化工具场景(以AutoHotkey为例)
结合窗口标题和控件特征双重验证:
IsComposeWindow() { WinGetTitle, activeTitle, A ; 先匹配标题关键词 if (InStr(activeTitle, "新建邮件") || InStr(activeTitle, "撰写")) { ; 再验证窗口内是否存在撰写相关控件(以Outlook收件人输入框为例) ControlExist, RichEdit20W1, %activeTitle% return ErrorLevel = 0 } return false }
内容的提问来源于stack exchange,提问作者Willy
相关产品推荐
相关产品推荐

