使用VBA,在编辑要发送的Outlook电子邮件时,如何捕获电子邮件中的文本。

huangapple go评论62阅读模式
英文:

How, using VBA, to capture the text in an Outlook email, whilst the email is being edited to send

问题

以下是您要翻译的内容:

所以,这是我想要实现的目标。

用户回复了一封电子邮件,并开始编写一些文本(要么在新窗口中,要么在预览窗口中)。我希望能够捕获这些新的文本。

我发现的是:

  • 如果用户突出显示了一些他们的新文本,我可以捕获这些突出显示的文本
  • 但是,如果我尝试捕获所有文本(即窗口中显示的整封电子邮件,包括新文本),我的代码只会捕获被回复的原始电子邮件中的文本,而不是新文本。

这是我已经拥有的代码,并附有注释说明它的功能。

Function GetCurrentMail() As Object
' 我使用这个函数来获取当前正在编辑的邮件对象,这个函数有效
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

这确实返回一些文本,但只返回被回复的原始电子邮件之前的文本,而不给我任何新文本。

因为我的突出显示的代码适用于新文本,所以我确信必须有一种通过VBA访问它的方法,但我束手无策!有谁可以帮忙吗?

英文:

So, here is what I want to achieve.

A user replies to an email, and starts writing some text (either in a new window or a preview window). I wish to be able to capture this NEW text.

What I have found is:

  • if the user highlights some of their new text, I can capture this highlighted text
  • but if I try to capture ALL the text (so the whole email as it appears in the window, inc the new text), my code will only capture the text from the original email that is being replied to, not the new text as well

Here's the code I have, with comments on what it does.

Function GetCurrentMail() As Object
&#39; 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 = &quot;Explorer&quot;
        Set CurrentMail = ActiveExplorer.Selection.Item(1)
    Case Is = &quot;Inspector&quot;
        Set CurrentMail = ActiveInspector.CurrentItem
    Case Else
        GoTo CE

End Select

If CurrentMail Is Nothing Or TypeName(CurrentMail) &lt;&gt; &quot;MailItem&quot; Then GoTo CE

Set GetCurrentMail = CurrentMail

CE:

End Function

And here is code I already have that will correctly capture any highlighted text, and this works on new text the user may have entered (if they highlight it)

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

But although the above correctly captures highlighted text, even if it is NEW text, I can't find a way to get ALL the new text (or indeed, the whole email, inc new text, and the original text)

I have tried:

GetCurrentMail.Body

This does return some text, but only the text from the original email before it was replied to, it does not give me any of the new text.

Because my highlighted code works with new text, I am sure there must be a way to access it via VBA, but I am stumped! Can anyone help please?

答案1

得分: 1

Outlook对象模型在UI中进行的更改在项目保存或焦点转移到UI中的另一个字段(例如,通过单击切换到“主题”行)之前,不会(或可能不会)传播到OOM,处理Outlook对象模型时已知的问题。

此外,在访问在Outlook中编辑的项目时有一些技巧。例如,要获取在Explorer窗口中正在编辑或撰写的项目,您需要使用Explorer.ActiveInlineResponse属性:

因此,不要使用以下代码:

Case Is = "Explorer"
        Set CurrentMail = ActiveExplorer.Selection.Item(1)

您需要使用:

Case Is = "Explorer"
        Set CurrentMail = ActiveExplorer.ActiveInlineResponse 

此外,我建议处理项目级事件,以便您可以在项目保存(或自动保存)后立即检查消息正文。有关更多信息,请参阅在每个检查器中实现包装器并跟踪项目级事件

但我建议处理Outlook Application类的[ItemSend][3]事件,该事件在Microsoft Outlook项目通过用户通过检查器(在检查器关闭之前,但在用户单击“发送”按钮之后)或在程序中使用Outlook项目(例如MailItem)的Send方法时触发。在事件处理程序中,您可以检查消息正文,并通过将Cancel参数设置为True来取消发送过程,如果需要的话。

英文:

The Outlook object model doesn't (or may not) propagate changes made in the UI to the OOM until the item is saved or focus is moved to another field in the UI (for example, switch from the body to the Subject line by clicking there). This is a known issue when dealing with the Outlook object model.

Also there are some tricks when accessing items edited in Outlook. For example, to get items that are being edited or composed in the Explorer window you need to use the Explorer.ActiveInlineResponse property:

So, instead of the following code:

Case Is = &quot;Explorer&quot;
        Set CurrentMail = ActiveExplorer.Selection.Item(1)

You need to use:

Case Is = &quot;Explorer&quot;
        Set CurrentMail = ActiveExplorer.ActiveInlineResponse 

Also I'd suggest handling the item-level events so you could check the message body right after the item is saved (or auto-saved). See Implement a wrapper for inspectors and track item-level events in each inspector for more information about that.

But I'd suggest handling the [ItemSend][3] event of the Outlook Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. In the event handler you may check the message body and cancel the sending process if required by setting the Cancel parameter to true.

huangapple
  • 本文由 发表于 2023年6月16日 01:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定