I would like, thanks to a code, that as soon as I receive an email from a certain person that the attachment of this email is automatically saved in a folder "TEST" here, then that the email is marked as read and then filed. Here is what I could find online but it does not work. I have no error message but I have no record either.
Can you please help me
Sub extrait_PJ_vers_rep(strID As Outlook.MailItem)
Dim olNS As Outlook.namespace
Dim MyMail As Outlook.MailItem
Dim expediteur
Set olNS = Application.GetNamespace("MAPI")
Set MyMail = olNS.GetItemFromID(strID.EntryID)
If MyMail.Attachments.Count > 0 Then
expediteur = MyMail.SenderEmailAddress
Repertoire = "c:\TEST" & "\"
If Repertoire <> "" Then
If "" = Dir(Repertoire, vbDirectory) Then
MkDir Repertoire
End If
End If
Dim PJ, typeatt
For Each PJ In MyMail.Attachments
typeatt = Isembedded(strID, PJ.Index)
If typeatt = "" Then
If "" <> Dir(Repertoire & PJ.FileName, vbNormal) Then
MsgBox Repertoire & PJ.FileName & " Done before"
If "" = Dir(Repertoire & "old", vbDirectory) Then
MkDir Repertoire & "old"
End If
FileCopy Repertoire & PJ.FileName, Repertoire & "old\" & PJ.FileName
End If
PJ.SaveAsFile Repertoire & PJ.FileName
End If
Next PJ
MyMail.UnRead = False
MyMail.Save
Dim myDestFolder As Outlook.MAPIFolder
Set myDestFolder = MyMail.Parent.Folders("test")
MyMail.Move myDestFolder
End If
Set MyMail = Nothing
Set olNS = Nothing
End Sub
I would like, thanks to a code, that as soon as I receive an email from a certain person that the attachment of this email is automatically saved in a folder "TEST" here, then that the email is marked as read and then filed
CodePudding user response:
The code does exactly what you need, you just need to hook up the NewMailEx
event of the Outlook Application class which is fired when a new message arrives in the Inbox and before client rule processing occurs. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem
, MeetingItem
, or SharingItem
. The EntryIDsCollection
string contains the Entry ID that corresponds to that item. Use the Entry ID returned in the EntryIDCollection array to call the
Then you can add the NewMailEx
event handler:
Viola! The event handler will be added, you just need to paste your code here.