1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| '~~> Set a reference to Microsoft Internet Controls
'~~> The GetWindow function retrieves the handle of a window that has
'~~> the specified relationship (Z order or owner) to the specified window.
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
'~~> The GetForegroundWindow function returns the handle of the foreground
'~~> window (the window with which the user is currently working).
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Sub GetURL()
Dim sw As SHDocVw.ShellWindows
Dim objIE As SHDocVw.InternetExplorer
Dim topHwnd As Long, nextHwnd As Long
Dim sURL As String, hwnds As String
Set sw = New SHDocVw.ShellWindows
'~~> Check the number of IE Windows Opened
'~~> If more than 1
hwnds = "|"
If sw.Count > 1 Then
'~~> Create a string of hwnds of all IE windows
For Each objIE In sw
hwnds = hwnds & objIE.hwnd & "|"
Next
'~~> Get handle of handle of the foreground window
nextHwnd = GetForegroundWindow
'~~> Check for the 1st IE window after foreground window
Do While nextHwnd > 0
nextHwnd = GetWindow(nextHwnd, 2&)
If InStr(hwnds, "|" & nextHwnd & "|") > 0 Then
topHwnd = nextHwnd
Exit Do
End If
Loop
'~~> Get the URL from the relevant IE window
For Each objIE In sw
If objIE.hwnd = topHwnd Then
sURL = objIE.LocationURL
Exit For
End If
Next
'~~> If only 1 was found
Else
For Each objIE In sw
sURL = objIE.LocationURL
Next
End If
MsgBox (sURL)
Debug.Print sURL
Set sw = Nothing: Set objIE = Nothing
End Sub |
Partager