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
|
Option Explicit
Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Private Sub CommandButton1_Click()
Dim monImage As String
Dim Sh As Shape
'Définit le nom et le lieu de stockage de l'image
monImage = ActiveWorkbook.Path & "\monimage" & ".jpg"
keybd_event vbKeySnapshot, 1, 0&, 0&
DoEvents
Range("A1").Select
ActiveSheet.Paste
'on rogne l'image: à toi de régler suivant ta convenance à l'aide de l'enregistreur de macro
With Selection
.ShapeRange.ScaleWidth 0.68, msoFalse, msoScaleFromTopLeft
.ShapeRange.ScaleHeight 0.68, msoFalse, msoScaleFromTopLeft
End With
'Récupère la dernière forme de la feuille
Set Sh = ActiveSheet.Shapes(ActiveSheet.Shapes.Count)
'Colle l'image dans un graphique
With ActiveSheet.ChartObjects.Add(0, 0, Sh.Width, Sh.Height).Chart
.Paste
'Sauvegarde l'image du graphique au format jpg
.Export monImage, "JPG"
End With
'Supprime le graphique et la forme.
With ActiveSheet
.ChartObjects(ActiveSheet.ChartObjects.Count).Delete
.Shapes(ActiveSheet.Shapes.Count).Delete
End With
Application.ScreenUpdating = True
MsgBox "L'image est sauvegardée dans le dossier du classeur."
End Sub |
Partager