How to make print screen of worksheet

It would be often useful to be able to take a screenshot automatically. Many times I was wondering if it is possible at all to code it. In this article I will share with You how can You make print screen of your worksheet.

make print screen of worksheet spiderman

Method I

The first idea was to simulate print screen action, using the key on keyboard. I tried to record it, but unfortunately the first line in the saved macro recording was ActiveSheet.Paste and I was not interested in it.

So I had to dug in net a little bit and found SendKeys action, which is, let’s call it, equivalent of print screen key.

Application.SendKeys "({1068})", True

It was really cool solution, but not always worked as it should have. And then I got to this topic, where the guy said about this issue and how he solved it with simple DoEvents.

Application.SendKeys "({1068})", True

Method II

During my researches I also came across with this.

Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
  bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

One simple line can turn this sub into screenshot.

keybd_event VK_SNAPSHOT, 1, 0, 0

What I also found was that few code lines, which use this sub, can capture the image of currently active application.

keybd_event VK_MENU, 0, 0, 0
keybd_event VK_SNAPSHOT, 0, 0, 0
keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0

To paste the taken print screen simply go the same way as in method I.


Get the object

After pasting the screen shot the best way to get it is to set it to the variable.

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

Of course remember, that ws must be set worksheet and AltPrintScreen set as shape.

Dim AltPrintScreen as Shape
Dim ws as Worksheet

Above ActiveSheet please change into your worksheet variable.
Also please specify as much variables as it is possible. Thanks to that your code is more stable and reliable.

To sum up

The dreams came true! Now You can actually automate making print screen of worksheet. What was only a wish before, now becomes real.

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

Leave a Reply

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