1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
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)
'Récupération de la date de réception du mail
Dim DateRecep As Date
Dim DateExcel As String
DateRecep = Format(MyMail.ReceivedTime, "dd/mm/yyyy")
DateExcel = Format(MyMail.ReceivedTime, "yyyymmdd")
'Si le mail est compris entre mardi et samedi alors on lance la macro sinon on supprime le mail
If Weekday(DateRecep, vbTuesday) < 6 Then
If MyMail.Attachments.Count > 0 Then
expediteur = MyMail.SenderEmailAddress
Repertoire = "c:\temp\pj\"
If Repertoire <> "" Then
If "" = Dir(Repertoire, vbDirectory) Then
MkDir Repertoire
End If
End If
'on traite les pj
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 & " existe !!"
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
End If
Set MyMail = Nothing
Set olNS = Nothing
'ouvrir excel
'Déclaration des variables
Dim appExcel As New Excel.Application 'Application Excel
Dim wbExcel As Excel.Workbook 'Classeur Excel
Dim wsExcel As Excel.Worksheet 'Feuille Excel
Const LANCE_EXCEL = "C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE /cmd/"
Const FICHIER_AR = " C:\projets\test.xlsm"
On Error Resume Next
Set wbExcel = appExcel.Workbooks.Open(FICHIER_AR)
Stop
While thisWBK.ReadOnly = True
Sleep 3000
appExcel.DisplayAlerts = False
wbExcel.Close False
Set wbExcel = Application.Workbooks.Open(FICHIER_AR)
Application.DisplayAlerts = True
Wend
'suite du code si ouverture en écriture
appExcel.Run wbExcel.Name & "!ouverture", Left(CStr(DateExcel), 10)
'où ouverture est le nom de ta macro
Else
MyMail.Delete
End If
Fin:
End Sub |
Partager