2

每当有人在 Outlook 中创建新电子邮件时,我都想运行 VBA 函数。这可能是因为他们点击了“新邮件”按钮,或者是因为第三方程序生成了一个新的邮件窗口。

我尝试使用 Application_ItemLoad() 并检查作为事件参数传递的 Item 对象,但是在 2007 年,我收到一条消息,上面写着“该项目的属性和方法不能在此事件过程中使用。” 显然这是 Outlook 2007 中的一个已知问题。

有没有人有一个可靠的方法来做到这一点?

谢谢,

史蒂夫

4

2 回答 2

2

在 Outlook 2010 中,它对我有用,但我没有找到错误报告。你的确切代码是什么?你是什​​么意思,“检查项目对象”?

您只需将代码放在这样的事件中:

Private Sub Application_ItemLoad(ByVal Item As Object)
    MsgBox "New mail item."
End Sub

就这样。

我希望MS提供的这些评论对您有用:http: //msdn.microsoft.com/en-us/library/office/ff868544.aspx

评论:

此事件在 Outlook 项目开始加载到内存时发生。该项目的数据尚不可用,除了 Outlook 项目的 Class 和 MessageClass 属性的值,因此在调用 Item 中返回的 Outlook 项目的 Class 或 MessageClass 以外的任何属性时会发生错误。同样,如果您尝试从 Outlook 项目调用任何方法,或者如果您在 Item 中返回的 Outlook 项目上调用 Application 对象的 GetObjectReference 方法,则会发生错误。

编辑
我能想到的最好的方法是将此代码放入 Application_ItemSend 事件方法中:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim myInspector
Dim wdDoc
Dim rng

Set myInspector = Item.GetInspector
Set wdDoc = myInspector.WordEditor

Set rng = wdDoc.Application.Selection

With rng
    With rng.Style.Font
        .Name = "Arial Black"
        .Size = 12
    End With
End With

Set myInspector = Nothing
Set wdDoc = Nothing

End Sub

问题是您无法在尚不可用的项目上设置属性(如 MS 所述)。好吧,从这个角度来看,这实际上是不可能的。

于 2012-10-24T09:10:18.140 回答
0

I came up with a way to do this for replies. I have not yet figured out how to do this for new compositions though.

Public WithEvents myExplorer As Outlook.Explorer 'Gives us the ability to trigger off explorer events
Public WithEvents myMailItem As Outlook.MailItem 'Gives us the ability to trigger off MailItem events

Private Sub Application_Startup()   
    Set myExplorer = Application.ActiveExplorer 'Initialize
End Sub

Private Sub myExplorer_SelectionChange()
    If (myExplorer.Selection.Count > 0) Then
        If (myExplorer.Selection.Item(1).Class = olMail) Then
            Set myMailItem = myExplorer.Selection.Item(1) 'Initialize
        End If
    End If
End Sub


Public Sub NewHTMLEmail(htmlToUse As String, Addressee As String, SubjectLine As String)
    Dim NewEmail As Outlook.MailItem
    Set NewEmail = Application.CreateItem(olMailItem)
    NewEmail.BodyFormat = olFormatHTML
    NewEmail.Subject = SubjectLine
    NewEmail.To = Addressee
    NewEmail.HTMLBody = htmlToUse
    NewEmail.Display
End Sub

'This trigger executes when we hit the "Reply" button while viewing a mail item
Private Sub myMailItem_Reply(ByVal Response As Object, Cancel As Boolean)
    Dim htmlString As String, thisMailItem As Outlook.MailItem, MissingText As String
    Set thisMailItem = Response
    Call NewHTMLEmail(thisMailItem.HTMLBody, thisMailItem.To, thisMailItem.Subject)

    Response.Close olDiscard
    thisMailItem.Delete    
End Sub
于 2013-10-11T14:07:34.620 回答