Waleed
Waleed

Reputation: 919

How to run a code after attachment file is opened on outlook?

I need to run a code after I open attachment on outlook itself (either opened manually or by vba).
I tried to use AttachmentRead event ,But I found out this event is triggered just after I double click on the attached file or using open from context menu (the attachment still not opened).
my need,to run a code after the attachment is fully opened by it's relative application.
In advance,grateful for all useful comments and answers.

Option Explicit
Option Compare Text
Public WithEvents MyItem As Outlook.MailItem

Private Sub Application_ItemLoad(ByVal Item As Object)
    If Item.Class = olMail Then
        Set MyItem = Item
    End If
End Sub

Private Sub MyItem_AttachmentRead(ByVal Attachment As Attachment)
     MsgBox ("This is a test message") 'This is a test code
End Sub

Upvotes: 0

Views: 141

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66356

Even if there was an event like that, Outlook would have no way to figure out whether the external application started up and opened the file: it gives the file to the Windows shell that takes it over and launches the application without waiting for the application to initialize and actually do anything with the file. You wouldn't want Outlook to wait for a few seconds while the external application takes its time, would you?

The best you can do is to track the MailItem.BeforeAttachmentWriteToTempFile event - but it is triggered both by opening and by previewing the attachment, and you won't know the file name unless you monitor the temp folder where Outlook creates the files.

Your best bet is to work with the application that actually opens the file.

Upvotes: 1

Related Questions