您可以使用 Application_ItemLoad 事件,但它可能比您想的要复杂一些 - 您需要将 Item 参数过滤到仅邮件项目,然后添加复杂的是,在该过程中,大多数 Item 属性都无法访问。
要解决属性访问问题,您可以将对象缓存在全局变量中并设置一个短的 Windows 计时器来调用子程序来处理该项目(如果您发现它看起来像一个新的未发送邮件对象,请添加附件)
在ThisOutlooksession:
Private Sub Application_ItemLoad(ByVal Item As Object)
If TypeOf Item Is MailItem Then
Set NewLoadedItem = Item
StartTimer
End If
End Sub
modHandleItemLoad"
Option Explicit
Public NewLoadedItem As MailItem
Sub ProcessLoadedItem()
If NewLoadedItem Is Nothing Then Exit Sub
With NewLoadedItem
'check here and add the attachment if appropriate
End With
Set NewLoadedItem = Nothing
End Sub
modTimer:
Option Explicit
Public Declare Function SetTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
Public TimerID As Long
Sub StartTimer()
TimerID = SetTimer(0&, 0&, 500&, AddressOf TimerProc)
End Sub
Sub EndTimer()
On Error Resume Next
KillTimer 0&, TimerID
End Sub
Sub TimerProc()
EndTimer 'stop the timer
ProcessLoadedItem
End Sub