Grab Address Bar (Current URL) From Active IE Window

micropathic

Well-known member
Joined
Oct 23, 2003
Messages
75
Hi, I am looking for a way to grab the ULR from the Address bar in an IE window. Does anyone know of a way I could do this?

Thanks for any help!
 
Well, I have been looking into doing this with Win32 API calls. Here is the code Ive been trying to make work:

Code:
Private Declare Function GETWINDOWTEXT Lib "user32"  Alias "GetWindowTextA"(ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function FindWindow Lib "user32"  Alias "FindWindowA"(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function FindWindowEx Lib "user32"  Alias "FindWindowExA"(ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
    	
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim sCaption As New VB6.FixedLengthString(256) Dim String w/ 256 Spaces In Memory, Is There A .Net Way To Do This?
    Dim hwnd As Long = FindWindow("IEFrame", vbNullString) Finds IE Window By Container Name, "IEFrame"
        
    If Not hwnd = 0 Then Tests If Window Exists
       SetForegroundWindow(hwnd) Brings Window To Foreground
       GETWINDOWTEXT(hwnd, sCaption.Value, 256) Get The Text From That Window, For Testing Code... Not Functionally Needed
       MsgBox(sCaption.Value) Show The Value Of The Caption, For Testing Code... Not Functionally Needed
   
   The Following 5 Lines Of Code Are What Seem To Be Causing An Issue.
   Im Trying To "Drill" Into The IE Window And Search For The URL Address
   Bar Portion Of The Window.  This Doesnt Seem To Be Working Correctly.
   What Am I Doing Wrong With These FindWindowExs?

    Dim Worker As Long = FindWindowEx(hwnd, 0, "WorkerW", vbNullString) Supposed To Find The Component W/ The Caption "WorkerW" Within The IE Window
    Dim ToolBar As Long = FindWindowEx(Worker, 0, "rebarwindow32", vbNullString) Supposed To Find The Component W/ The Caption "rebarwindow32" Within The "WorkerW" Component
    Dim ComboBoxEx As Long = FindWindowEx(ToolBar, 0, "comboboxex32", vbNullString) Supposed To Find The Component W/ The Caption "comboboxex32" Within The "rebarwindow32" Component
    Dim Combo As Long = FindWindowEx(ComboBoxEx, 0, "combobox", vbNullString) Supposed To Find The Component W/ The Caption "combobox" Within The "comboboxex32" Component
    Dim Edit As Long = FindWindowEx(Combo, 0, "Edit", vbNullString) Supposed To Find The Component W/ The Caption "Edit" (This Is Actually The URL Address Bar From Which Im Trying To 
                                                                    Pull The Text From) Within The "combobox" Component
 
    Here Is Where I Would Like To Pull The Text From The "Edit" 
    Component (URL Address Bar Text) And Then Show The Value In
    A Message Box
     
    End If

End Sub
At this point I am a little lost, so any help would be super valuable to me!
 
Last edited by a moderator:
Got this to work with this code:

Code:
Private Declare Function GETWINDOWTEXT Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Private Const WM_GETTEXT As Short = &HDS
Private Const WM_GETTEXTLENGTH As Short = &HES

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim hwnd As Integer = FindWindowEx(0, 0, "IEFrame", vbNullString)
      
        If Not hwnd = 0 Then
            SetForegroundWindow(hwnd)

            Dim Worker As Integer = FindWindowEx(hwnd, 0, "WorkerW", vbNullString)
            Dim ToolBar As Integer = FindWindowEx(Worker, 0, "ReBarWindow32", vbNullString)
            Dim ComboBoxEx As Integer = FindWindowEx(ToolBar, 0, "ComboBoxEx32", vbNullString)

            Dim txtLength As Long = SendMessage(ComboBoxEx, WM_GETTEXTLENGTH, CInt(0), CInt(0)) + 1   Get Length Of Text
            Dim txtBuff As String = Space(txtLength)
            Dim URL As Long = URL = SendMessage(ComboBoxEx, WM_GETTEXT, txtLength, txtBuff) Get URL From ComboBoxEx
           
            MsgBox(txtBuff)
        End If
End Sub
 
Back
Top