Y a-t-il un moyen de prendre une capture d'écran dans MS-Access avec vba?

je veux utiliser vba pour prendre une capture d'écran (qui sera ensuite envoyée en pièce jointe). Idéalement, j'aimerais prendre une capture d'écran de seulement la forme active. Est-il possible de faire cela?

11
demandé sur dmr 2010-03-16 21:19:49

2 réponses

pour ce faire, vous devez utiliser les appels de L'API Windows. Le code suivant fonctionne dans MS Access 2007. Il sauvegardera les fichiers BMP.

Option Compare Database
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 Const VK_SNAPSHOT = &H2C

Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long

Private Declare Function CloseClipboard Lib "user32" () As Long

Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" _
(PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, _
IPic As IPicture) As Long

'\ Declare a UDT to store a GUID for the IPicture OLE Interface
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

'\ Declare a UDT to store the bitmap information
Private Type uPicDesc
    Size As Long
    Type As Long
    hPic As Long
    hPal As Long
End Type

Private Const CF_BITMAP = 2
Private Const PICTYPE_BITMAP = 1

Sub PrintScreen()
    keybd_event VK_SNAPSHOT, 1, 0, 0
End Sub

Public Sub MyPrintScreen(FilePathName As String)

    Call PrintScreen

    Dim IID_IDispatch As GUID
    Dim uPicinfo As uPicDesc
    Dim IPic As IPicture
    Dim hPtr As Long

    OpenClipboard 0
    hPtr = GetClipboardData(CF_BITMAP)
    CloseClipboard

    '\ Create the interface GUID for the picture
    With IID_IDispatch
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With

    '\ Fill uPicInfo with necessary parts.
    With uPicinfo
        .Size = Len(uPicinfo) '\ Length of structure.
        .Type = PICTYPE_BITMAP '\ Type of Picture
        .hPic = hPtr '\ Handle to image.
        .hPal = 0 '\ Handle to palette (if bitmap).
    End With

   '\ Create the Range Picture Object
   OleCreatePictureIndirect uPicinfo, IID_IDispatch, True, IPic

    '\ Save Picture Object
    stdole.SavePicture IPic, FilePathName

End Sub

Il y a un article de la base de connaissances qui va en profondeur.

10
répondu Raj More 2010-03-16 19:18:47

Utilisez l'exemple de raj pour obtenir l'image et puis ceci pour sauver

Dim oPic
On Error Resume Next
Set oPic = Clipboard.GetData
On Error GoTo 0
If oPic Is Nothing Then
  'no image in clipboard'
Else
  SavePicture oPic, "c:\temp\pic.bmp"
end if
1
répondu bugtussle 2010-03-16 18:53:38