There is an Application_ItemLoad
event which you could use, but it would be a bit more complex than you might like - you'd need to filter down the Item
argument to only Mail Items, and then there's the added complication that within that procedure most of the Item
properties are not accessible.
To get around the property access problem you can cache the object in a global variable and set a short Windows Timer to call a sub to process the item (adding the attachment if you find it looks like a new unsent mail object)
In 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