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 88 89 90 91 92
| Option Explicit
' https://www.developpez.net/forums/d2088646/logiciels/microsoft-office/excel/macros-vba-excel/capture-ecran-userform/#post11605894
#If VBA7 Then
Private Declare PtrSafe Sub keybd_event Lib "User32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)
Private Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function EmptyClipboard Lib "User32" () As LongPtr
Private Declare PtrSafe Function CloseClipboard Lib "User32" () As LongPtr
Private Declare PtrSafe Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As LongPtr
Private Declare PtrSafe Function CountClipboardFormats Lib "User32" () As LongPtr
#Else
Private Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function OpenClipboard Lib "User32" (ByVal hWnd As Long) As Long
Private Declare Function EmptyClipboard Lib "User32" () As Long
Private Declare Function CloseClipboard Lib "User32" () As Long
Private Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As Long
Private Declare Function CountClipboardFormats Lib "User32" () As Long
#End If
'=====================================================================
'- SUBROUTINE : PRINT SCREEN & PASTE INTO THE WORKSHEET
'- events and SendKeys work slowly, so need lots of delays in the code
'- ALT+PrtScrn copies active window
'- PrtScrn copies the entire desktop
'=====================================================================
Sub CopyUserFormToWorksheet()
Dim i As Integer
ClearClipboard
CopyWndwAsImg
i = 1
Do While IsClipboardEmpty()
DoEvents
Debug.Print i
i = i + 1
Loop
'---------------------------------------------------------------
'- paste picture to worksheet
ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
'Sheets("Feuil1").Paste Range("A10")
DoEvents
End Sub
'=====================================================================
Sub ShowUserForm()
Dim i As Integer
Dim deb As Double, fin As Double
Dim hbm As Long
deb = Timer * 1000#
UserForm1.Show (False)
Application.Wait Now + TimeValue("00:00:02")
ClearClipboard
deb = Timer * 1000#
CopyWndwAsImg
i = 1
Do While IsClipboardEmpty()
DoEvents
Debug.Print i
i = i + 1
Loop
fin = Timer * 1000#
Debug.Print "Temps capture écran : ", fin - deb
ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
'Sheets("Feuil1").Paste Range("A10")
End Sub
Sub CopyWndwAsImg()
'---------------------------------------------------------------------------------------
keybd_event vbKeyMenu, 0, 0, 0
keybd_event vbKeySnapshot, 0, 0, 0
keybd_event vbKeySnapshot, 0, 2, 0
keybd_event vbKeyMenu, 0, 2, 0
DoEvents
End Sub
Private Function IsClipboardEmpty() As Boolean
IsClipboardEmpty = (CountClipboardFormats() = 0)
End Function
Private Sub ClearClipboard()
OpenClipboard 0&
EmptyClipboard
CloseClipboard
DoEvents
End Sub |