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 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String _
, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Function FichierExiste(MonFichier As String)
'par Excel-Malin.com ( [http://excel-malin.com] )
If Len(Dir(MonFichier)) > 0 Then
FichierExiste = True
Else
FichierExiste = False
End If
End Function
Sub ImprimerFichier()
Dim NomFichier1 As String
Dim NomFichier2 As String
Dim NomFichier3 As String
Dim x As Long
Dim nbLignes As Integer
Dim I As Integer
Dim ObjOutlook As New Outlook.Application
Dim oBjMail
Dim Nom_Fichier As String
Dim Nom_Fichier_2 As String
Set ObjOutlook = New Outlook.Application
Set oBjMail = ObjOutlook.CreateItem(olMailItem)
x = FindWindow("XLMAIN", Application.Caption)
Range("A2").Select
nbLignes = Range("A2", Selection.End(xlDown)).Cells.Count
For I = 2 To nbLignes
Dim MonFichier As String
MonFichier = "P:\BE\DOSSIER_FAB\" & Cells(I, 1) & ".pdf"
If FichierExiste(MonFichier) = True Then
NomFichier1 = "C:\Users\sleroy\Desktop\Impression OF\" & Cells(I, 2) & ".pdf"
ShellExecute x, "print", NomFichier1, False, False, 1
NomFichier2 = "C:\Users\sleroy\Desktop\Impression OF\Enregistrement Contrôles.pdf"
ShellExecute x, "print", NomFichier2, False, False, 1
NomFichier3 = "P:\BE\DOSSIER_FAB\" & Cells(I, 1) & ".pdf"
ShellExecute x, "print", NomFichier3, False, False, 1
Else
Nom_Fichier = "C:\Users\ccc\Desktop\Impression OF\" & Cells(I, 2) & ".pdf"
With oBjMail
.To = "méthodes@ccc.fr" ' le destinataire
.Subject = "OF Jaune/OF vert" ' l'objet du mail
.Body = "Bonjour," & vbCrLf & vbCrLf & "Les plans de ces OFs ne se trouvent pas dans la base de données accessible par la logistique. Merci de préparer ces OFs (vert ou jaune). OFs en pièces jointes. " & vbCrLf & vbCrLf & "Cdt," 'le corps du mail ..son contenu
.Attachments.Add Nom_Fichier '"C:\Data\essai.txt" ' ou Nomfichier
End With
End If
Next
With oBjMail
.Send
End With
Set oBjMail = Nothing
Set ObjOutlook = Nothing
End Sub |
Partager