VBA Screen Capture

menor59

Well-known Member
Joined
Oct 3, 2008
Messages
572
Office Version
  1. 2021
Platform
  1. Windows
Ok im lost...this should be working

Code:
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 KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12




Sub Screen_Capture_VBA()
 Dim Sec3 As Date, PasteYes As Integer
 MsgBox "Three seconds after you click OK " & _
 "the active window will be copied to the clipboard."
 Sec3 = DateAdd("s", 2, Now)
 Do Until Now > Sec3
 DoEvents
 Loop
    ' Run ScreenCapture Sub Routine
 ScreenCapture
    '------------------------------------
 PasteYes = MsgBox("Click OK to paste the screen capture in VBA.", _
 vbOKCancel)
 If PasteYes = 1 Then


    'Paste Image in Chart and Export it to Image file.
    Charts.Add
    ThisWorkbook.Charts(1).AutoScaling = True
    ThisWorkbook.Charts(1).Paste
    ThisWorkbook.Charts(1).Export Filename:="C:\ScreenCapture\ClipBoardToPic.jpg", FilterName:="jpg"
 
    'Supress warning message and Delete the Chart
    Application.DisplayAlerts = False
    ThisWorkbook.Charts(1).Delete
    Application.DisplayAlerts = True
 
    'Attach the file to Excel
    Attach_File
    '----------------------------------
    ThisWorkbook.Activate
    ThisWorkbook.Sheets(1).Select
 End If
End Sub


Sub ScreenCapture()
 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
End Sub
  
Sub Attach_File()
    ActiveCell.Select
    ActiveSheet.OLEObjects.Add(Filename:="C:\ScreenCapture\ClipBoardToPic.jpg", Link:=False, _
        DisplayAsIcon:=True, IconFileName:= _
        "C:\Program Files\Internet Explorer\iexplore.exe", IconIndex:=10, IconLabel _
        :="ClipBoardToPic.jpg").Select
End Sub

When i run it it finishes but dosent create the image as an embeded object.. What am i doing wrong?? Please advice or Correct...
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,196,017
Messages
6,012,872
Members
441,737
Latest member
bijayche

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top