Bonjour,
Une solution possible :
Sub SauvegardeCaptureEcran()
Dim ShDonnees As Worksheet
Dim ShChObj As ChartObject
Dim Chemin As String, DateDeCreation As String, CheminFichier As String
Dim HeureEnCours As Variant
DateDeCreation = Year(Date) & "-" & Format(Month(Date), "00") & "-" & Format(Day(Date), "00")
HeureEnCours = Split(Time, ":")
Chemin = ThisWorkbook.Path & "\" ' A adapter
Set ShDonnees = Sheets("Capture Ecran")
With ShDonnees
CheminFichier = Chemin & DateDeCreation & " " & Join(HeureEnCours, "-") & " " & .Range("NomFichier") & " " & DecompteCaptures(Chemin, "jpg", DateDeCreation) & ".jpg"
Set ShChObj = .ChartObjects.Add(.Range("J6").Left, .Range("J6").Top, .Shapes(1).Width, .Shapes(1).Height)
.Shapes(1).CopyPicture
Application.DisplayAlerts = False
With ShChObj
.Activate
.Chart.Paste
.Chart.Export CheminFichier
.Delete
End With
Set ShChObj = Nothing
Application.DisplayAlerts = True
End With
MsgBox "Capture sauvegardée : " & Chr(10) & CheminFichier, vbCritical
Set ShDonnees = Nothing
End Sub
Function DecompteCaptures(ByVal Chemin2 As String, ByVal Extension As String, ByVal Gdh As String) As Integer
Dim Fso, F, F1, Fc
DecompteCaptures = 0
Set Fso = CreateObject("Scripting.FileSystemObject")
Set F = Fso.GetFolder(Chemin2)
Set Fc = F.Files
For Each F1 In Fc
If Fso.GetExtensionName(F1) = Extension Then
If InStr(1, F1.Name, Gdh, vbTextCompare) > 0 Then
DecompteCaptures = DecompteCaptures + 1
End If
End If
Next
Set Fso = Nothing: Set F = Nothing: Set Fc = Nothing
End Function
La macro est lancée par le bouton vert dans la barre d'accès rapide :