How to screen capture of active application?

Last time I showed You how to make preview of a PDF file. Unfortunately that method puts whole file into your worksheet. In case of big size, it increases a lot the workbook weight. In this article I’m going to show You how to make it easier and lighter by screen capture of active application.

Screen capture of active application

I got many complains about the final size of the workbook. Big PDF files make preview worksheet really heavy. In that time I suggested to make smaller preview files, while I was searching for the “lighter” solution.

I decided to use II method (keybd_event) from my print screen article. The only thing I had to add to this was code to be sure that opened PDF file will be the one activated.

To achieve this I chose Shell function, mainly from 2 reasons.

The first one was that I could open the file and get its process identifier (PID).

PID = Shell("""" & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" _
& """" & " " & """" & StrFile & """", 3)

The second was that I could get that object into WScript.shell object.

Set WshShell = CreateObject("WSCript.shell")

And check if file is active.

Do Until WshShell.AppActivate(PID)
Application.Wait Now + TimeValue("0:00:01")
Loop

Additionally, after screenshots, Shell function gave me possibility to close opened file.

Call Shell("TaskKill /F /PID " & CStr(PID), vbHide)

The code

For my purposes I made a function, where ws is worksheet for print screens and StrFile is PDF file path. Also what is worth to mention, in the end of the function the code set to AltPrintScreen (so to the function) the last shape of the given worksheet.

With ws
    Set AltPrintScreen = .Shapes(.Shapes.Count)
End With

This get the captured screen into object and the function returns it under its name.

Option Explicit

Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
  bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12

Function AltPrintScreen(ws As Worksheet, StrFile As String)

Dim WshShell As Object
Dim PID As Long, i As Long

Application.CutCopyMode = False
Application.ScreenUpdating = False

PID = Shell("""" & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" _
& """" & " " & """" & StrFile & """", 3)

Set WshShell = CreateObject("WSCript.shell")

Do Until WshShell.AppActivate(PID)
Application.Wait Now + TimeValue("0:00:01")
Loop
Application.Wait Now + TimeValue("0:00:01")

keybd_event VK_MENU, 0, 0, 0
keybd_event VK_SNAPSHOT, 0, 0, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
Application.Wait Now + TimeValue("0:00:01")

ws.Paste

Call Shell("TaskKill /F /PID " & CStr(PID), vbHide)

With ws
    Set AltPrintScreen = .Shapes(.Shapes.Count)
End With

End Function

To sum up

Maybe it is slower than before, but in terms of overall preview file size it is much better. Do You know the struggle of opening Excel file, which bigger than 20 Mb? Multiply it couple of times in case of PDF previews. Sometimes it is better to wait and have usable file.

But yeah, that was it! This was how to screen capture of active application! Really easy and light alternative for PDF previews by pasting whole file object.

Tomasz Płociński

Author: Tomasz Płociński

Excel VBA enthusiast who is also open for other languages. Mainly working in VBA, some SQL, hungry for more.

Leave a Reply

Your email address will not be published. Required fields are marked *