PDA

View Full Version : API show last active URL



Emily
02-15-2007, 08:56 PM
The API code at the bottom can show the last active URL in IE, Nestcape, ... (The first message box) BUT it cannot show Firefox's URL.

The IF statement works

Code:


If InStr(Title, "- Mozilla Firefox") Then
GetIEurl (Hwnd)


Any idea?

Thanks in advance
Emily


Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowTextA Lib "user32" (ByVal Hwnd As Long, ByVal lpString _
As String, ByVal cch As Long) As Long
Declare Function SendMessageA Lib "user32" (ByVal Hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Declare Function GetClassNameA Lib "user32" (ByVal Hwnd As Long, ByVal lpClassName _
As String, ByVal nMaxCount As Long) As Long
Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long

Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2

Sub Main()
Dim Hwnd As Long
Dim Title As String
Dim TitleL As Integer
Hwnd = GetDesktopWindow
Hwnd = GetWindow(Hwnd, GW_CHILD)
Do While Hwnd <> 0
If GetParent(Hwnd) = 0 Then
Title = Space(256)
TitleL = GetWindowTextA(Hwnd, Title, 256)
Title = Left$(Title, TitleL)
If InStr(Title, "- Netscape") Or InStr(Title, "Tencent Traveler") Or _
InStr(Title, "TheWorld") Or InStr(Title, " - Microsoft Internet Explorer") Or _
InStr(Title, "- Mozilla Firefox") Then
GetIEurl (Hwnd)
End If
End If
Hwnd = GetWindow(Hwnd, GW_HWNDNEXT)
Loop
End Sub

Function GetClassName(Hwnd As Long) As String
Dim TempStr As String
Dim Rc As Long
TempStr = Space(260)
Rc = GetClassNameA(Hwnd, TempStr, Len(TempStr))
GetClassName = StrConv(LeftB$(StrConv(TempStr, vbFromUnicode), Rc), vbUnicode)
End Function

Function GetIEurl(H As Long) As String
Dim IeHwnd As Long
Dim cHwnd As Long
Dim UrlLen As Long
Dim IEurl As String
IeHwnd = GetWindow(H, GW_CHILD)
Do While IeHwnd <> 0
Debug.Print GetClassName(IeHwnd)
If GetClassName(IeHwnd) = "Edit" Then
UrlLen = SendMessageA(IeHwnd, WM_GETTEXTLENGTH, 0, 0)
If UrlLen <> 0 Then
UrlLen = UrlLen + 1
IEurl = Space$(UrlLen)
Call SendMessageA(IeHwnd, WM_GETTEXT, UrlLen, ByVal IEurl)
MsgBox Left$(IEurl, UrlLen)
Exit Do
End If
End If
cHwnd = GetWindow(H, GW_CHILD)
If cHwnd <> 0 Then GetIEurl IeHwnd
IeHwnd = GetWindow(IeHwnd, GW_HWNDNEXT)
Loop
End Function


Xtreme CrossPost on 10 Feb 07: http://www.xtremevbtalk.com/showthread.php?t=279452

Norie
02-15-2007, 10:46 PM
Emily

What are you actually trying to do?

And what's the Excel connection?

Emily
02-16-2007, 05:24 AM
Hi Horie

Actually no relation to Excel, just want to understand why API not works for Firefox. Also I cannot find the correct post area here. Xtreme readers seem no interest to the above API.

I missed the condition, there are more than one IE or/and Netscape or/and Firefox Windows in process.

Charlize
02-16-2007, 05:53 AM
I believe that for IE 7 it will not work. The string title is "- Windows Internet Explorer" and not InStr(Title, " - Microsoft Internet Explorer") and maybe the syntax is if InStr(Len(Title)-Len(" - Microsoft Internet Explorer"),Title, " - Microsoft Internet Explorer") <> 0 then because format is "Instr(startposition,string active,string we want)"

Charlize

Ivan F Moala
02-16-2007, 03:42 PM
Emily
The class name is MozillaWindowClass for FireFox
It is not easy with FireFox.
SPY++ will NOT reveal the tree you need to go down,
you have to get the parent and all child windows which have the same name.
So you need to iterate the child windows to find the child [toolbar]...then to that object's child [combo box] and finally to that child's editable text object.

Also the combobox has the name "Location" : combo box
SPY++ will not reveal this..

Also as Charlize suggested IE7 is diff text so use last part instead.

stanl
02-16-2007, 05:33 PM
Why not just Set oShell=CreateObject("Shell.Application"); then iterate oShell.Windows(), and obtain each Window Items LocationURL property?

Stan

oops.. forgot for IE (don't know about the others) you can use the Shell.Application, the ssfHISTORY NameSpace, then the GetDetailsof() method to obtain folder items. I just finished a script that places this in a new Excel sheet.

Emily
02-16-2007, 07:21 PM
Thanks Ivan

Hi Stanl, would you please give me an example, thanks

Ivan F Moala
02-16-2007, 09:25 PM
Why not just Set oShell=CreateObject("Shell.Application"); then iterate oShell.Windows(), and obtain each Window Items LocationURL property?

Stan

oops.. forgot for IE (don't know about the others) you can use the Shell.Application, the ssfHISTORY NameSpace, then the GetDetailsof() method to obtain folder items. I just finished a script that places this in a new Excel sheet.

Stan, good suggestion .... but won't work for FireFox

stanl
02-17-2007, 05:16 AM
Stan, good suggestion .... but won't work for FireFox

Hmmmm. then that leaves determining the preferred browser and going after it's index.dat file.... then may be irrelevant for multiple tabs in a browser. There is a Mozilla.ocx that simulates IE's browser object, at least enough to capture the URL... that involves GetObject() and we come back to Norie's comment, what does this have to do with Excel?:bug: Stan

Emily
02-17-2007, 06:29 AM
Hi Stan

The question is only for interest

Quoted from #3


Actually no relation to Excel, just want to understand why API not works for Firefox. Also I cannot find the correct post area here. Xtreme readers seem no interest to the above API.