如何用VBA代码恢复Outlook休眠后窗口的指定排列顺序?
用VBA重新排列Outlook窗口
可以通过Outlook VBA实现窗口排序,让任务栏悬停显示的窗口按「收件箱→日历→按日期排序的邮件窗口」排列,以下是具体代码和说明:
完整VBA代码
Sub ReorderOutlookWindows() Dim objOutlook As Outlook.Application Dim objExplorer As Outlook.Explorer Dim objInspector As Outlook.Inspector Dim colExplorers As Outlook.Explorers Dim colInspectors As Outlook.Inspectors Dim arrExplorers As Collection Dim arrInspectors As Collection Dim i As Integer Dim tempItem As Object Dim j As Integer Set objOutlook = Outlook.Application Set colExplorers = objOutlook.Explorers Set colInspectors = objOutlook.Inspectors Set arrExplorers = New Collection Set arrInspectors = New Collection ' 收集所有资源管理器窗口(收件箱、日历等文件夹窗口) For Each objExplorer In colExplorers arrExplorers.Add objExplorer Next objExplorer ' 收集所有检查器窗口(单独打开的邮件窗口) For Each objInspector In colInspectors arrInspectors.Add objInspector Next objInspector ' 激活收件箱窗口,固定其在预览列表的首位 For i = 1 To arrExplorers.Count Set objExplorer = arrExplorers(i) If objExplorer.CurrentFolder.DefaultItemType = olMailItem And objExplorer.CurrentFolder.Name = "收件箱" Then objExplorer.Activate Exit For End If Next i ' 激活日历窗口,固定其在收件箱之后 For i = 1 To arrExplorers.Count Set objExplorer = arrExplorers(i) If objExplorer.CurrentFolder.DefaultItemType = olAppointmentItem Then objExplorer.Activate Exit For End If Next i ' 对邮件窗口按接收日期降序排序 For i = 1 To arrInspectors.Count - 1 For j = i + 1 To arrInspectors.Count If arrInspectors(i).CurrentItem.ReceivedTime < arrInspectors(j).CurrentItem.ReceivedTime Then Set tempItem = arrInspectors(i) arrInspectors.Remove i arrInspectors.Add tempItem, , j End If Next j Next i ' 依次激活排序后的邮件窗口,确定其预览顺序 For Each objInspector In arrInspectors objInspector.Activate Next objInspector ' 释放对象资源 Set objOutlook = Nothing Set colExplorers = Nothing Set colInspectors = Nothing Set arrExplorers = Nothing Set arrInspectors = Nothing End Sub
关键逻辑说明
- 窗口分类处理:Outlook窗口分为两类——
Explorer(文件夹窗口,如收件箱、日历)和Inspector(单独打开的邮件窗口),代码分别收集这两类窗口进行操作。 - 固定核心窗口顺序:通过依次激活收件箱、日历窗口,利用任务栏预览列表的「激活顺序关联规则」,将这两个窗口固定在前列。
- 邮件窗口排序:使用冒泡排序对邮件窗口按接收时间降序排列,再依次激活,确保最新邮件排在预览列表的邮件区域最前面(需升序则将代码中的
<改为>)。
使用注意事项
- 代码部署:按
Alt + F11打开Outlook VBA编辑器,插入模块后粘贴代码。 - 宏权限设置:在「文件→选项→信任中心→信任中心设置→宏设置」中选择「启用所有宏」或「通知所有宏」,确保代码可运行。
- 自定义适配:若收件箱名称非中文「收件箱」(如英文"Inbox"),需修改代码中
objExplorer.CurrentFolder.Name = "收件箱"的名称匹配值。 - 自动触发:若需休眠唤醒后自动执行,可结合Windows任务计划触发该宏(需保证Outlook处于运行状态)。
内容的提问来源于stack exchange,提问作者Allan Bertelsen
相关产品推荐
相关产品推荐

