0

I have this module for outlook:

Sub NewMessageWithAttachment()
    Dim oMsg As Outlook.MailItem
    Set oMsg = Application.CreateItem(olMailItem)
    With oMsg
        .Attachments.Add "C:\Users\Vinicius\image"
        .Display
    End With
End Sub

But everytime i want to run it, requires to press f5 or start it, is there anyway i can make it run automatically every time that i press to compose a new mail ?

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Related: https://stackoverflow.com/questions/13041614/trigger-function-to-run-when-a-user-starts-writing-a-new-email-in-outlook-vba – Tim Williams May 27 '21 at 20:44
  • Does this answer your question? [Fire an Outlook 2003 macro when the user creates a new blank message](https://stackoverflow.com/questions/3674832/fire-an-outlook-2003-macro-when-the-user-creates-a-new-blank-message) – niton May 28 '21 at 15:49

4 Answers4

1

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
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
0

You can repurpose the corresponding ribbon button. So, when a user clicks on the ribbon button for creating a new email your event handler will be triggered before. See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information.

Another approach for handling reply, replyall and forward events is to handle the SelectionChange event of the Explorer class. So, you will be aware which item is currently selected in the Explorer window and will be able to subscribe to the item-level events. You may find the Implement a wrapper for inspectors and track item-level events in each inspector article helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

There is no event like that available in the Outlook.Application object. The closest thing I could find is on MailItem.Write event. This would allow you to attach at save, or autosave, before a mailitem is sent. You can also capture the .Send event.

HackSlash
  • 4,944
  • 2
  • 18
  • 44
0

Already got;


Private Sub Application_ItemLoad(ByVal Item As Object)
    If (TypeOf Item Is MailItem) Then
        Set myItem = Item
    End If
End Sub

Private Sub myItem_Open(Cancel As Boolean)
    myItem.Subject = ""
    myItem.To = ""
    myItem.Attachments.Add "C:\Users\Public\Documents\teste.xlsx"

End Sub

it'll cancel the create new mail event, and instead open a personalized one with the attachment.