Determining top most Internet Explorer window. (top most)

Posted on 2004-11-04
Last Modified: 2008-02-20
having a little trouble with this one.

I created an ie toolbutton app, from which I need to get the URL of the browser that launched my app from.

Here is the form startup code I have so far...

Dim SWs As New SHDocVw.ShellWindows
Dim tmpIE As SHDocVw.InternetExplorer
  For Each tmpIE In SWs
  txtURL.Text = tmpIE.LocationURL
End If

this works ok, but if they have more than one broswer open, I get the url from the browser that
navigated last, I need to get the URL from the browser they launched my app from, or what should be the current top most IE window. I have looked into functions to determine this, but they are all C++ examples which is foriegn to me.

Also my app runs as topmost, however I could set this after I have pulled the URL if I have to.

Question by:flosoft
    LVL 9

    Expert Comment

    by:Shahid Thaika
    I have answered a similar question recently. Though it still hasn't been answered as yet, I am sure it is a good answer.

    Hope it helps answer your question as well.

    Author Comment

    there is alot there. Guess I was hoping someone had written an easy function I could call
    from the above code, something like

    Dim SWs As New SHDocVw.ShellWindows
    Dim tmpIE As SHDocVw.InternetExplorer
      For Each tmpIE In SWs
    if IsTopMost(tmpIE) then
      txtURL.Text = tmpIE.LocationURL
    End if
    End If

    I will look through that page some more but it will take some time to sort out what portions I would need and would not because there is alot more there than I need.

    LVL 32

    Accepted Solution

    Give this a try...

    Option Explicit

    Private Declare Function GetForegroundWindow Lib "user32" () As Long

    Sub Command1_Click()

        Dim shWnds As New ShellWindows
        Dim ie As InternetExplorer
        Dim lhWnd As Long
        Dim sURL As String
        sURL = ""
        'Loop all the instances of IE and see if one has focus
        For Each ie In shWnds
            'Get the handle of the window with focus
            lhWnd = GetForegroundWindow()
            'See if any IE windows have the focus
            If (ie.hWnd = lhWnd) Then
                '---===### This one has focus ###===---
                sURL = ie.LocationURL
            End If

    End Sub

    Author Comment

    Sweet, Thanks!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now