Option Explicit
Option Compare Text
Sub Imprimer()
Dim Sh As Worksheet, Proceed As Boolean, ShToExport, Elem
Dim Fichier_traité As String, Chemin As String, Psw As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
ThisWorkbook.Save ' On va rajouter des onglets fantômes alors on sauvegarde avant
Psw = "."
Chemin = ThisWorkbook.Path & "\"
Fichier_traité = Dir(Chemin & "*.xls*")
ShToExport = Array("Inspection machine", "Résumé")
Do While Fichier_traité <> ""
If Fichier_traité <> ThisWorkbook.Name Then
Proceed = True
With Workbooks.Open(Chemin & Fichier_traité)
' le Titre est une formule vers le nom de fichier initial : on ne conserve que le Texte
With .Worksheets("Inspection machine")
.UnProtect Psw
.[A1] = .[A1].Value
End With
.Worksheets(ShToExport).Copy _
After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
.Close False
End With
Else
ThisWorkbook.Worksheets(ShToExport).Copy _
After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
End If
Fichier_traité = Dir
Loop
If Proceed Then
For Each Sh In ThisWorkbook.Worksheets
For Each Elem In ShToExport
If InStr(1, Sh.Name, Elem & " (", vbTextCompare) Then
Sh.Select Replace:=False
Exit For
End If
Next
Next
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Chemin & Format(Date, "YYYYMMDD") & " - " & "Rapport d'inspection.pdf", _
OpenAfterPublish:=True
ActiveWindow.SelectedSheets.Delete
Worksheets(ShToExport(0)).Activate
End If
ThisWorkbook.Saved = True ' La situation devrait être celle de la sauvegarde en début de sub
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub