当用户开始在Outlook-VBA中写入新电子邮件时运行的触发器函数

当用户开始在Outlook-VBA中写入新电子邮件时运行的触发器函数,vba,outlook,Vba,Outlook,每当有人在Outlook中创建新电子邮件时,我希望运行VBA函数。这可能是因为他们点击“新建邮件”按钮,或者是因为第三方程序生成了一个新的邮件窗口 我尝试过使用应用程序_ItemLoad()并检查作为事件参数传递的项对象,但是在2007年,我收到一条消息说“此事件过程中无法使用项的属性和方法”。显然,这是Outlook 2007中的一个已知问题 有人有可靠的方法来做这件事吗 谢谢 史蒂夫在Outlook 2010中,它对我有效,我没有发现bug报告。你的确切密码是什么?“检查项目对象”是什么意思

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

我尝试过使用应用程序_ItemLoad()并检查作为事件参数传递的项对象,但是在2007年,我收到一条消息说“此事件过程中无法使用项的属性和方法”。显然,这是Outlook 2007中的一个已知问题

有人有可靠的方法来做这件事吗

谢谢


史蒂夫

在Outlook 2010中,它对我有效,我没有发现bug报告。你的确切密码是什么?“检查项目对象”是什么意思

您只需将代码放入事件中,如下所示:

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

我希望my女士提供的这些评论对您有所帮助:

备注:

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

编辑
我能想到的最好办法是将以下代码放入应用程序\u 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所述)。那么从这个角度来看呢实际上是不可能的

我想出了一个方法来回复。不过,我还没有弄明白如何在新作品中做到这一点

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

我有一个启动新电子邮件的第三方应用程序(如中所示,打开一个撰写电子邮件的窗口)。然而,它的字体是我的公司不喜欢的。他们希望我这样做,当电子邮件窗口打开时,字体会自动更改为Calibri。是的,这也是我一直碰到的墙。感谢您的确认。请参见此处的NewInspector事件示例