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()

Selecting a cell in VBA

1: How to Select a Cell on the Active Worksheet

To select cell D5 on the active worksheet, you can use :

ActiveSheet.Cells(5, 4).Select
-or-
ActiveSheet.Range("D5").Select
    

2: How to Select a Cell on Another Worksheet in the Same Workbook

To select cell E6 on another worksheet in the same workbook, you can use :

Application.Goto ActiveWorkbook.Sheets("Sheet2").Cells(6, 5)
-or-
Application.Goto (ActiveWorkbook.Sheets("Sheet2").Range("E6"))
    
Or, you can activate the worksheet, and then use method 1 above to select the cell:

Sheets("Sheet2").Activate
ActiveSheet.Cells(6, 5).Select
    

3: How to Select a Cell on a Worksheet in a Different Workbook

To select cell F7 on a worksheet in a different workbook, you can use :

Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Cells(7, 6)
-or-

Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("F7")
    
Or, you can activate the worksheet, and then use method 1 above to select the cell:

Workbooks("BOOK2.XLS").Sheets("Sheet1").Activate
ActiveSheet.Cells(7, 6).Select 
 

One

Hello World.