Home > Net >  How to take screenshot of application and save it word using VBA
How to take screenshot of application and save it word using VBA

Time:04-20

I have a VBA code to open the Attachmate Reflection(IBM Screen). I want to take complete screenshot of the window(like print screen) and paste the screenshot into word document. However, I am not able to take print screen and paste it in word.

Getting "object property or method not supported" for objWord.Paste line

    'Reflection screen part
    Set view = frame.CreateView(terminal)
    Set screen = terminal.screen
    
    ...
    
' word document
Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)  

     

       Set objWord = CreateObject("Word.Application")
       Set objDoc = objWord.Documents.Add
       objWord.Visible = True
       
       Set para = objDoc.Paragraphs.Add
       para.Range.Text = Inp_Str & vbCrLf & vbCrLf & vbCrLf
       para.Range.ParagraphFormat.SpaceAfter = 10
        
       objDoc.Paragraphs.Add
       objDoc.Paragraphs.Add.Range.Text = "Line 2 hello"
        
       **Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
       
       'Paste into Word    This paste is not working
        objWord.Paste**
       
       'quit the word application:
       objWord.Quit

CodePudding user response:

objWord.Paste should be changes to objWord.Selection.Paste. I also needed Sleep to give keybd_event time to copy the screenshot to the clipboard.

Test

Private Declare PtrSafe 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 = &H2C
Private Declare PtrSafe Function Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) As Long

Sub Test()
    Const Inp_Str As String = "Hello World"
    Dim objWord As Object
    Dim objDoc As Object
    Dim para As Object
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
       
    Set para = objDoc.Paragraphs.Add
    para.Range.Text = Inp_Str & vbCrLf & vbCrLf & vbCrLf
    para.Range.ParagraphFormat.SpaceAfter = 10
        
    objDoc.Paragraphs.Add
    objDoc.Paragraphs.Add.Range.Text = "Line 2 hello"
        
    keybd_event VK_SNAPSHOT, 0, 0, 0
    Sleep 500
       
    'Paste into Word    This paste is not working
    objWord.Selection.Paste
       
    'quit the word application:
    objWord.Quit
End Sub
  • Related