Sunday, September 15, 2013

ScreenCapture of an Active Window in Word VBA.

If You want to take screenshots of a application automatically using VBA in Ms Word use the below macro in Word:

It simply open the Program mentioned in Shell Command and takes the screenshot of the opened application and paste it in Word Document.


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  As Long = &H2C '44
Private Const VK_LMENU As Long = &HA4 '164
Private Const KEYEVENTF_KEYUP As Long = 2
Private Const KEYEVENTF_EXTENDEDKEY As Long = 1
Private Declare Sub Sleep Lib "kernel32.dll" ( _
    ByVal dwMilliseconds As Long)

Sub test()



   Shell "cmd", vbNormalFocus        ' It simply opens the Command Pompt
   Sleep 2000& ' pause 2 seconds while the cmd window opens
   CopyWindow
   Selection.Paste


   Shell "C:\Program Files\Internet Explorer\iexplore.exe ", vbNormalFocus      ' It opens the Internet Explorer
   Sleep 15000& ' pause 15 seconds while the IE Browser window opens
   CopyWindow
   Selection.Paste

   Shell "notepad", vbNormalFocus
   Sleep 10000& ' pause 10 seconds while the notepad window opens
   CopyWindow
   Selection.Paste

End Sub

Sub CopyWindow()
   DoEvents
   ' Alt PrtScn
   keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0&    ' key down
   keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0&
   keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0&
   keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0&
   DoEvents

End Sub

No comments:

Post a Comment