当用户回复电子邮件并开始编写一些文本(在新窗口或预览窗口中)时,我希望捕获此新文本。
我发现:
我的代码,带注释。
Function GetCurrentMail() As Object
' I use this to get the current mail object that is being edited, this works
Dim CurrentMail As Object
Select Case TypeName(ActiveWindow)
Case Is = "Explorer"
Set CurrentMail = ActiveExplorer.Selection.Item(1)
Case Is = "Inspector"
Set CurrentMail = ActiveInspector.CurrentItem
Case Else
GoTo CE
End Select
If CurrentMail Is Nothing Or TypeName(CurrentMail) <> "MailItem" Then GoTo CE
Set GetCurrentMail = CurrentMail
CE:
End Function
用于捕获任何突出显示文本的代码。这适用于用户可能输入的新文本。
Function GetHighlighted() As String
Dim olInsp As Object
Dim wdDoc As Object
Dim strText As String
With GetCurrentMail
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
strText = wdDoc.Application.Selection.Range.Text
End With
GetHighlighted = strText
Set olInsp = Nothing
Set wdDoc = Nothing
End Function
如何获取所有新文本(或整封电子邮件、新文本和原始文本)。
我尝试过:
GetCurrentMail.Body
这将返回原始电子邮件在回复之前的文本。
Outlook 对象模型不会(或可能不会)将 UI 中所做的更改传播到 OOM,直到保存项目或将焦点移动到 UI 中的另一个字段(例如,从正文切换到
Subject
单击此处即可行)。这是处理 Outlook 对象模型时的一个已知问题。
访问在 Outlook 中编辑的项目时也有一些技巧。例如,要获取正在资源管理器窗口中编辑或撰写的项目,您需要使用 Explorer.ActiveInlineResponse 属性:
因此,代替以下代码:
Case Is = "Explorer"
Set CurrentMail = ActiveExplorer.Selection.Item(1)
您需要使用:
Case Is = "Explorer"
Set CurrentMail = ActiveExplorer.ActiveInlineResponse
此外,我建议处理项目级事件,以便您可以在保存项目(或自动保存)后立即检查消息正文。有关详细信息,请参阅为检查器实现包装器并跟踪每个检查器中的项目级事件。
但我建议处理 Outlook
[ItemSend][3]
类的 Application
事件,每当用户通过 Inspector
发送 Microsoft Outlook 项目时就会触发该事件(在检查器关闭之前,但在用户发送之后)单击 Send
按钮)或在程序中使用 Outlook 项目的 Send
方法(例如 MailItem
)时。在事件处理程序中,您可以检查消息正文,并根据需要将 Cancel
参数设置为 true 来取消发送过程。