0

I am creating a macro that will capture a screen with keyboard events. After that, it will save the screenshot to an Excel file.

Here is the code:

DoEvents
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0&
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0&
DoEvents

capturesFile.Worksheets(capturesFile.Worksheets.Count).Paste Destination:=curWS.Cells(rowNum + 2, 2)

I want my macro to save the same screenshot as a JPG file in a folder, before saving it to the Excel file.

More information: I am capturing any random screen (Desktop, Skype, Outlook, some folder, some Web page, it could be anything.) I want to save the screenshot to a folder. Saving the screenshot as a JPG file has no relation with the Excel file.

Can you please help me?

  • Does this answer your question? [Excel VBA save screenshot](https://stackoverflow.com/questions/34062242/excel-vba-save-screenshot) – Pᴇʜ Jun 09 '20 at 05:58
  • Hello PEH! Thanks for your comment. I am capturing any random screen (Desktop, Skype, Outlook, some folder, some Web page, it could be anything.) I want to save the screenshot to a folder. Saving the screenshot as a JPG file has no relation with the Excel file. Do you know how that can be done? –  Jun 09 '20 at 06:12

2 Answers2

1

I had small difficulties with the solution of PEH, so I used his method to make this version. On my computer (Window 10, Office 2010 is running well.)

Sub SaveScreenshotAsJpeg()
    'make your screenshot here (so it is in the clipboard) …
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets.Add
    ws.Paste ' Past to the worksheet. It is a shape.
    Dim TempPicture As Shape
    On Error Resume Next ' Test if it succedded. Clipboard could be empty or text ...
        Set TempPicture = ws.Shapes(1)
    On Error GoTo 0
    If TempPicture Is Nothing Then
        MsgBox "Pasting picture was not successfull (not on clipboard). End."
        ws.Delete ' delete the unused worksheet
        Exit Sub
    End If
    TempPicture.CopyPicture 'copy again, since it was removed from clipboard.
    'create a chart with the exact size of the picture
    Dim TempChart As ChartObject
    Set TempChart = ws.ChartObjects.Add(0, 0, TempPicture.Width, TempPicture.Height)
    With TempChart.Chart 'paste the screenshot into the chart
        .ChartArea.Select
        .Paste
    End With
    'export the chart
    TempChart.Chart.Export Filename:="C:\Temp\test.jpg", FilterName:="JPEG"
    ws.Delete ' delete the unused worksheet with all its objects
End Sub
Viktor West
  • 544
  • 6
  • 9
  • Thanks PEH for providing the basic code, but unfortunately, it did not work on my computer. Thanks a lot Viktor for modifying PEH's code! Your code works on my computer. I appreciate your help :) –  Jun 09 '20 at 09:35
0

Since I believe there is no direct way to save this into a file, there is the following workaround:

  1. We insert the picture into a worksheet (can be any worksheet or a temporary).
  2. We read the width and height of the picture (and delete it).
  3. We create a chart with the exact size of the picture.
  4. We paste the picture into that chart.
  5. We export that chart to jpeg.

Why such a complicated workaround?

  • We cannot save pictures directly, but charts have an export method (so we can use that as a trick).
  • We need to know the width and height to create the chart in the correct size (otherwise your picture is stretched.

Option Explicit

Sub SaveScreenshotAsJpeg()

    'make your screenshot here (so it is in the clipboard) …

    Dim ws As Worksheet
    Set ws = Tabelle1 'define your sheet (this can be a temporary sheet)

    'paste the screenshot as picture
    ws.Range("A1").PasteSpecial

    Dim TempPicture As Object
    Set TempPicture = Selection

    'get height and width of the picture
    Dim PicWidth As Double
    PicWidth = shp.width
    Dim PicHeight As Double
    PicHeight = shp.Height

    'delete the picture
    TempPicture.Delete

    'create a chart with the exact size of the picture
    Dim TempChart As ChartObject
    Set TempChart = Tabelle1.ChartObjects.Add(0, 0, PicWidth, PicHeight)

    'paste the screenshot into the chart
    TempChart.Chart.Paste

    'export the chart
    TempChart.Chart.Export Filename:="C:\Temp\test.jpg", FilterName:="JPEG"

    'delete the chart
    objchart1.Delete
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73