'### Constante à adapter ###
Const MON_IMAGE As String = "C:\ma plage sélectionnée.jpg"
'###########################
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function SetCursorPos& Lib "user32" (ByVal x As Long, ByVal y As Long)
Private Declare Function GetMessageExtraInfo& Lib "user32" ()
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
'//////////////////////////////////////////////
'/// NE PAS LANCER LA MACRO A PARTIR DU VBE ///
'//////////////////////////////////////////////
Sub Selection2JPG()
Dim C As CommandBar
Dim B As CommandBarButton
Dim S1 As Worksheet
Dim S2 As Worksheet
Dim S As Shape
Dim CO As ChartObject
Dim x&
Dim y&
Application.ScreenUpdating = True
Application.WindowState = xlMaximized
With ActiveWorkbook.Windows(1)
.WindowState = xlMaximized
x& = .Width \ 2
y& = .Height \ 2
End With
Set S1 = ActiveSheet
Set S2 = Sheets.Add
Set C = CommandBars.Add
Set B = C.Controls.Add(Type:=msoControlButton, ID:=280)
S1.Activate
B.Execute
S2.Activate
SetCursorPos x&, y&
Call mouse_event(MOUSEEVENTF_LEFTDOWN, x&, y&, 0, GetMessageExtraInfo) '
Call mouse_event(MOUSEEVENTF_LEFTUP, x&, y&, 0, GetMessageExtraInfo)
DoEvents
C.Delete
Set S = S2.Shapes(1)
With S
.CopyPicture
Set CO = S2.ChartObjects.Add(.Left, .Top, .Width, .Height)
End With
With CO.Chart
.Paste
.Export Filename:=MON_IMAGE
End With
Application.DisplayAlerts = False
S2.Delete
Application.DisplayAlerts = True
End Sub