Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to take a screenshot in MS-Access with vba?

I want to use vba to take a screenshot (which will then be sent as an email attachment). Ideally, I'd like to take a screenshot of just the active form. Is there any way to do this?

like image 763
dmr Avatar asked Mar 16 '10 18:03

dmr


People also ask

How to print screen to an image using Access VBA?

Wiki > TechNet Articles > Print Screen to an Image using Access VBA There have been times, when you need to perform a Print Screen. The conventional method would be to use (ALT +)Print Scrn, Open MS Paint, CTRL + V, Then Save it by giving it a name, then get back to what you were doing.

How to take a screenshot of the current application window?

PrtSc takes a screenshot of the entire desktop. To take a screenshot of the current application window use Alt + PrtSc. Thank you so much for your reply. It is working. I also have an issue with the same code.

How to take a screenshot in Excel 32 bit?

You can try this code in a standard Module in Excel 32 Bit. Screenshots can be captured immediately by calling Sub prcSave_Picture_Screen and it will capture your whole screen and save to the same path as your workbook (You can change the path and file name if you want)

How to take high-resolution screenshots in Windows?

Now press Ctrl + S in order to save your newly captured high-resolution screenshot in Windows. Now, will tell you how you can take high-resolution screenshots with the help of the snipping tool in Windows.


2 Answers

You have to use Windows API calls to do this. The following code works in MS Access 2007. It will save BMP files.

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

There is a Knowledge Base article that goes into more depth.

like image 92
Raj More Avatar answered Sep 27 '22 17:09

Raj More


Use raj's example to get the image and then this to save

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
like image 45
bugtussle Avatar answered Sep 27 '22 17:09

bugtussle