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
| Option Explicit
Private Sub Application_Startup()
Dim appE As Excel.Application, wbk As Workbook, wsh As Worksheet
Dim Fichier As String, NomFeuille As String
Dim Datas As Variant, i As Long, DL As Long
Dim adresse As String, poste As String, NomAppareil As String, ReferenceApp As String, JourRestant As Integer
Const NOMFICHIER As String = "Moyen étalonnage2.xlsx"
Fichier = "C:\Users\" & Environ("username") & "\Desktop\" & NOMFICHIER
NomFeuille = "Feuil1"
'Ouverture application excel (invisible) + classeur
Set appE = New Excel.Application
appE.Visible = False
Set wbk = appE.Workbooks.Open(Fichier)
Set wsh = wbk.Sheets(NomFeuille)
With wsh
DL = derlig_reelle(.Columns(1))
'Extraction des données
Datas = .Range("A1:K" & DL).Value
End With
'Traitement
If Datas(2, 11) <> Date Then
'Boucle sur les valeurs de la colonne 7
For i = 0 To UBound(Datas)
If Datas(i, 6) <= 30 Then
adresse = Datas(i, 9) 'email dans la "colonne 10" de la variable tableau
poste = Datas(i, 0) 'poste d'utilisation dans la "colonne 1" de la variable tableau
NomAppareil = Datas(i, 1) 'type d'appareil dans la "colonne 2" de la variable tableau
ReferenceApp = Datas(i, 2) 'ref de l'appareil dans la "colonne 3" de la variable tableau
JourRestant = Datas(i, 6) 'nb de jour dans la "colonne 7" de la variable tableau
Call EnvoiMail(adresse, poste, NomAppareil, ReferenceApp, JourRestant)
End If
Next i
End If
wsh.Range("K2").Value = Date
'fermeture classeur + application
wbk.Close True
appE.Quit
Set wsh = Nothing
Set wbk = Nothing
Set appE = Nothing
End Sub
Private Sub EnvoiMail(adresse As String, poste As String, NomAppareil As String, ReferenceApp As String, JourRestant As Integer)
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
objMsg.Importance = olImportanceHigh
objMsg.To = adresse & ""
objMsg.Subject = "Appareil à étalonner"
objMsg.Body = "Bonjour," & Chr(13) & Chr(13) & "Attention : il ne reste que " & JourRestant & " pour étalonner la " & NomAppareil & " référencée : " & RefAppareil & " (utilisée sur le poste : " & poste & " )" & Chr(13)
objMsg.Display 'Display affiche; .send pour envoi sans relecture
Set objMsg = Nothing
End Sub
Private Function derlig_reelle(Plage As Range) As Long
If WorksheetFunction.CountA(Plage) = 0 Then derlig_reelle = Plage.Cells(1, 1).Row: Exit Function
derlig_reelle = Plage.Find("*", , , , , xlPrevious).Row
End Function |