I want to iterate through IE (internet explorer) tabs and take screen shots of specific windows and paste them in excel sheet.
I have written below code -
************************************************** ***
Expand|Select|Wrap|Line Numbers
- Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
- Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
- Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
- Private Declare Function GetForegroundWindow Lib "user32" () As Long
- Const SW_SHOW = 5
- Private Sub CommandButton1_Click()
- Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Picture"
- GetURL ("https://www.google.co.in/")
- End Sub
- Sub GetURL(url As String)
- Dim sw As SHDocVw.ShellWindows
- Dim objIE As SHDocVw.InternetExplorer
- Dim topHwnd As Long, nextHwnd As Long
- Dim sURL As String, hwnds As String
- Set sw = New SHDocVw.ShellWindows
- Set wsDest = Sheets("Picture")
- wsDest.Activate
- wsDest.Range("A1").Activate
- '~~> Check the number of IE Windows Opened
- '~~> If more than 1
- hwnds = "|"
- If sw.Count > 1 Then
- '~~> Create a string of hwnds of all IE windows
- For Each objIE In sw
- If Not (objIE.Name = "Windows Explorer") Then
- hwnds = hwnds & objIE.hWnd & "|"
- End If
- Next
- '~~> Get handle of handle of the foreground window
- nextHwnd = GetForegroundWindow
- '~~> Check for the 1st IE window after foreground window
- Do While nextHwnd > 0
- nextHwnd = GetWindow(nextHwnd, 2&)
- If InStr(hwnds, "|" & nextHwnd & "|") > 0 Then
- topHwnd = nextHwnd
- Exit Do
- End If
- Loop
- '~~> Get the URL from the relevant IE window
- For Each objIE In sw
- If objIE.hWnd = topHwnd And objIE.LocationName = "Google" Then
- Dim IEHandler As Long
- IEHandler = objIE.hWnd
- SetForegroundWindow IEHandler,
- ShowWindow IEHandler, SW_SHOW
- Call CaptureScreen(29, 215, 1287, 426)
- wsDest.Pictures.Paste.Select
- 'Exit For
- End If
- Next
- Else
- For Each objIE In sw
- sURL = objIE.LocationURL
- Next
- End If
- Debug.Print sURL
- Set sw = Nothing: Set objIE = Nothing
- End Sub
Here the issue is -
If 10 IE tabs are opened out of which 5 are for www.Google.com and want to take screen shot of those 5 tabs then I am 1st colleting handler of all 10 windows and after that checking the handler name and location name if both mathces then want to set those tabs foreground and activate those tabs.
But since handler value for all tabs are same (as it is opend in IE) able to foreground the window but not able to Activate correct window
SetForegroundWindow IEHandler,
ShowWindow IEHandler, SW_SHOW