每当有人在 Outlook 中创建新电子邮件时,我想运行 VBA 函数。发生这种情况的原因可能是他们单击了“新邮件”按钮,或者是因为第三方程序生成了新邮件窗口。
我有一个第三方应用程序,可以启动新电子邮件(打开一个用于撰写电子邮件的窗口)。我的公司希望字体自动更改为 Calibri。
我尝试了 Application_ItemLoad()
并检查作为事件参数传递的 Item 对象。
2007年我得到了
"The item's properties and methods cannot be used inside this event procedure."
显然这是 Outlook 2007 中的一个已知问题。
最佳答案
在 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 所描述)。那么,从这个角度来说,其实是不可能的。
关于vba - 触发函数,更改字体,当 Outlook 电子邮件创建时 : "The item' s properties and methods cannot be used inside this event procedure. ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13041614/