473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Setfocus to IE windows using excel macro

Hi,
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
  1. Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
  2. Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
  3. Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
  4. Private Declare Function GetForegroundWindow Lib "user32" () As Long
  5. Const SW_SHOW = 5
  6.  
  7. Private Sub CommandButton1_Click()
  8. Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Picture"
  9. GetURL ("https://www.google.co.in/")
  10. End Sub
  11.  
  12. Sub GetURL(url As String)
  13.     Dim sw As SHDocVw.ShellWindows
  14.     Dim objIE As SHDocVw.InternetExplorer
  15.     Dim topHwnd As Long, nextHwnd As Long
  16.     Dim sURL As String, hwnds As String
  17.  
  18.     Set sw = New SHDocVw.ShellWindows
  19.  
  20.     Set wsDest = Sheets("Picture")
  21.     wsDest.Activate
  22.     wsDest.Range("A1").Activate
  23.     '~~> Check the number of IE Windows Opened
  24.     '~~> If more than 1
  25.     hwnds = "|"
  26.     If sw.Count > 1 Then
  27.         '~~> Create a string of hwnds of all IE windows
  28.         For Each objIE In sw
  29.            If Not (objIE.Name = "Windows Explorer") Then
  30.             hwnds = hwnds & objIE.hWnd & "|"
  31.             End If
  32.         Next
  33.  
  34.         '~~> Get handle of handle of the foreground window
  35.         nextHwnd = GetForegroundWindow
  36.  
  37.         '~~> Check for the 1st IE window after foreground window
  38.         Do While nextHwnd > 0
  39.             nextHwnd = GetWindow(nextHwnd, 2&)
  40.             If InStr(hwnds, "|" & nextHwnd & "|") > 0 Then
  41.                 topHwnd = nextHwnd
  42.                 Exit Do
  43.             End If
  44.         Loop
  45.  
  46.         '~~> Get the URL from the relevant IE window
  47.         For Each objIE In sw
  48.             If objIE.hWnd = topHwnd And objIE.LocationName = "Google" Then
  49.                 Dim IEHandler As Long
  50.                 IEHandler = objIE.hWnd
  51.  
  52.                 SetForegroundWindow IEHandler, 
  53.                 ShowWindow IEHandler, SW_SHOW
  54.  
  55.                 Call CaptureScreen(29, 215, 1287, 426)
  56.                 wsDest.Pictures.Paste.Select
  57.                 'Exit For
  58.             End If
  59.         Next
  60.  
  61.     Else
  62.         For Each objIE In sw
  63.             sURL = objIE.LocationURL
  64.         Next
  65.     End If
  66.  
  67.     Debug.Print sURL
  68.  
  69.     Set sw = Nothing: Set objIE = Nothing
  70. End Sub
  71.  
*********************End of Code**************
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
Jun 6 '13 #1
0 1259

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mrs Howl | last post by:
I don't know if there's even a way to do what I want. I click on a button in an Access form, and it opens an instance of Excel, and opens a workbook and runs a macro that's in it. So far, fine,...
0
by: Alex | last post by:
i have a module in Access which opens an existing Excel file and envokes a macro within the Excel file to draw graphs. now i am trying to convert the Excel macro to an Access one so that the...
6
by: geronimo_me | last post by:
Hi, I am trying to run an Excel macro from an Access module, however when I run the code the macro runs but then I get an error in Access. The error is: Run-time error "440", Automation error. ...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
2
by: cr113 | last post by:
I just upgraded from Office 2000 to Office 2003. My VB.NET Excel macro calls don't work any longer. Here is how I make my Excel macro call from VB.NET: Dim objExcel as Excel.Application ...
1
by: cr113 | last post by:
We've upgraded from Office 2000 to Office 2003 and now my excel macro calls are messed up. There's about a 15 second delay from the time I make the call in VB.NET to the time the excel macro is...
3
by: akristensen | last post by:
I am new to this site, so be patient if I do not ask the question correctly. Current Target Platform: Browser: MS IE, script language: Javascript (will use VBScript, but JS is preferred), External...
1
by: Shishir01 | last post by:
Hi, I want to make a xml document using Excel macros. The code should read the cell contents of the excel worksheet and generate tags accordingly.
1
MitchR
by: MitchR | last post by:
Good Morning Folks; I have a question that is pretty far fetched but here goes nothing... I am looking to find a way to insert a macro into an Excel command button located in an Access VBA...
0
by: Michael mealin | last post by:
Hi guys. I am trying to fuly automate a cut and paste operation. Basically the data is dragged off the net, formatted using a macro in word then copied from word and pasted into Excel. From there...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.