Reputation: 31
I am trying to take screenshot of the web page and paste it in Word. Following is the code written
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Const VK_SNAPSHOT As Byte = 44
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_SHOWMAXIMIZED = 3
Private Const VK_LCONTROL As Long = &HA2
Private Const VK_V = &H56
Private Const KEYEVENTF_KEYUP = &H2
Sub Sample()
Dim objIE As InternetExplorerMedium
Set objIE = New InternetExplorerMedium
objIE.Visible = True
objIE.navigate "https://staging-site.com/"
Do
DoEvents
Loop Until objIE.readyState = 4
Dim hwnd As Long, IECaption As String
'~~> Get the caption of IE
IECaption = objIE.document.Title
'~~> Get handle of IE
hwnd = FindWindow(IECaption, vbNullString)
If hwnd = 0 Then
MsgBox "IE Window Not found!"
Exit Sub
Else
'~~> Maximize IE
ShowWindow hwnd, SW_SHOWMAXIMIZED
End If
Sleep 3000
DoEvents
'~~> Take a snapshot
Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
Dim SystemDateTime As String
sPath = Environ("USERPROFILE") & "\Desktop"
Set wordobj = CreateObject("Word.Application")
Set objDoc = wordobj.Documents.Add
SystemDateTime = Replace(Replace(Now, "/", ""), ":", "")
objDoc.SaveAs (sPath & "\Student Blue Waiver " & SystemDateTime)
wordobj.Visible = True
Set objSelection = wordobj.Selection
'Paste into Word
objSelection.Paste
objDoc.Save
End Sub
But the below variable 'hwnd' always returns 0 even though IE windows with caption is visible
hwnd = FindWindow(IECaption, vbNullString)
If hwnd = 0 Then
MsgBox "IE Window Not found!"
Exit Sub
I think IECaption is getting only the title of the window that might be an issue to FindWindow with desired title
Upvotes: 0
Views: 2196
Reputation: 53663
It looks like you're passing wrong arguments to the FindWindow
API function
The first argument should be "IEFrame"
and the second argument should be the window name. In your code, you're passing a the doc title as classname and an empty string (vbNullString
) as the window name!
NB I used CreateObject
to instantiate InternetExplorer.Application class. This may be slightly different from your InternetExplorerMedium
, I don't know.
I also found that I have to append " - Internet Explorer"
to the caption value, otherwise it can't be found.
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Sub foo()
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.navigate "http://cnn.com"
Do
DoEvents
Loop Until objIE.readyState = 4
Dim hwnd As Long, IECaption As String
'~~> Get the caption of IE
IECaption = objIE.Document.Title & " - Internet Explorer"
'~~> Get handle of IE
hwnd = FindWindow("IEFrame", IECaption)
If hwnd = 0 Then
MsgBox "IE Window Not found!"
Exit Sub
Else
Upvotes: 1