I've spent several hours today trying to figure this out, but I just can't get it. What I want to be able to do is get the current address (and page title if possible) from IE and throw it in a listbox on my form.
Thanks in advance!
Printable View
I've spent several hours today trying to figure this out, but I just can't get it. What I want to be able to do is get the current address (and page title if possible) from IE and throw it in a listbox on my form.
Thanks in advance!
Sure. But you would have to use APIs. Here's whow to get the current URL and a Page Title:
Also, you can add a reference to Microsoft Internet Controls (shdocvw.dll) to control the IE the way you want it.Code:Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Const WM_GETTEXTLENGTH = &HE
Private Const WM_GETTEXT = &HD
Private Sub Command1_Click()
Dim lngIE As Long
Dim lngWrkArea As Long
Dim lngEditBox As Long
Dim strEditText As String
Dim lngEditLen As Long
Dim lngCaptionLen As Long
Dim strCaption As String
lngIE = FindWindowEx(0, 0, "IEFrame", vbNullString)
lngWrkArea = FindWindowEx(lngIE, 0, "WorkerW", vbNullString)
lngWrkArea = FindWindowEx(lngWrkArea, 0, "ReBarWindow32", vbNullString)
lngWrkArea = FindWindowEx(lngWrkArea, 0, "ComboBoxEx32", vbNullString)
lngWrkArea = FindWindowEx(lngWrkArea, 0, "ComboBox", vbNullString)
lngEditBox = FindWindowEx(lngWrkArea, 0, "Edit", vbNullString)
lngEditLen = SendMessage(lngEditBox, WM_GETTEXTLENGTH, 0, 0)
strEditText = Space(lngEditLen)
If SendMessageStr(lngEditBox, WM_GETTEXT, lngEditLen, ByVal strEditText) Then
strEditText = Left(strEditText, InStr(strEditText, vbNullChar) - 1)
End If
lngCaptionLen = GetWindowTextLength(lngIE)
strCaption = Space(lngCaptionLen + 1)
If GetWindowText(lngIE, strCaption, lngCaptionLen + 1) Then
strCaption = Left(strCaption, InStr(strCaption, "- Microsoft Internet Explorer") - 1)
End If
MsgBox "The current URL: " & strEditText & vbCrLf & _
"Page Title: " & strCaption
End Sub
You're great Serge! Thanks so much. Originally, I was trying to use SendMessages with CB_GETLBTEXT and that did nothing.
The only thing: I had to change "WorkerW" to "WorkerA" because that's what I found in Spy++. Are there other possibilities on different systems with different versions of IE?
It should also be noted, I had to change all instances of lngEditLen to lngEditLen + 1.
lngEditLen = SendMessage(lngEditBox, WM_GETTEXTLENGTH, 0, 0)
strEditText = Space(lngEditLen + 1)
If SendMessageStr(lngEditBox, WM_GETTEXT, lngEditLen + 1, ByVal strEditText) Then
strEditText = Left(strEditText, InStr(strEditText, vbNullChar))
End If
Otherwise, it works like a charm!
I'm confused, it seems to work for everybody except for me :(
It does display the Title but not the URL, and unfortunatly that's the only thing i need can anyone please help?
Look at this thread which gwdash has answered. He shows how to get the url from IE. I tried the code and it does work.