MikeJoel
02-18-2008, 05:14 PM
I am starting out with http://www.xtremevbtalk.com/showthread.php?t=30761&highlight=Internet+Explorer
I declare,
Dim WithEvents IEObj As SHDocVw.InternetExplorer
Dim WithEvents SWObj As SHDocVw.ShellWindows
Then I use the "For Each" loop to find the window that has focus (also making sure it is actually a browser and not explorer),
'Create the Obj object of IE
Dim Obj As SHDocVw.InternetExplorer
'Loop through and find the window that has the
'current focus
For Each Obj In SWObj
If (Obj.hWnd = GetForegroundWindow) Then
If (InStr(1, Trim(LCase(Obj.FullName)), "iexplore.exe", vbTextCompare) > 0) Then
CurrentFocusWindow = Obj.LocationURL
If (CurrentFocusWindow <> LastFocusWindow) Then
UpdateBrowserObjects Obj
Exit For
End If
End If
End If
Next
'Clear the Obj
Set Obj = Nothing
(CurrentFocusWindow and LastFocusWindow are used to make sure the focus is changing to a different control, the CurrentFocusWindow is set to the obj.locationURL of the window with the current focus. While LastFocusWindow is set only when the app successfully changes to a browser)
Now, as you see if everything is right then I pass Obj through the sub UpdateBrowserObjects....
Public Sub UpdateBrowserObjects(Obj As InternetExplorer)
tmrCheckWindowFocus.Enabled = False
If (Obj.hWnd = GetForegroundWindow) Then
' Set the WebDoc conrol so we can control the page
' Set mhtmDoc3 = Nothing
' Set WebDoc = Nothing
Set mhtmDoc3 = Obj.document
Set WebDoc = mhtmDoc3
Set IEObj = Obj
LastFocusWindow = Obj.LocationURL
' Stop
Beep
Text2.Text = WebDoc.location
' Exit For
End If
'Next
Set Obj = Nothing
tmrCheckWindowFocus.Enabled = True
End Sub
(sorry for the commenting out of a lot of stuff, I have been trying to fix it)
(just ignore the Set mhtmDoc3 = Obj.document, Set WebDoc = mhtmDoc3, I am using that to give me access to the documents and events)
Now comes where it errors....
If I have two or more browsers open before the app starts I can switch between them fine. But if I close any of them, or open a new one I get an
Automation Error at
Set IEObj = Obj above in the UpdateBrowserObjects sub.
Why is this, any ideas
Thanks for any help
Mike
I declare,
Dim WithEvents IEObj As SHDocVw.InternetExplorer
Dim WithEvents SWObj As SHDocVw.ShellWindows
Then I use the "For Each" loop to find the window that has focus (also making sure it is actually a browser and not explorer),
'Create the Obj object of IE
Dim Obj As SHDocVw.InternetExplorer
'Loop through and find the window that has the
'current focus
For Each Obj In SWObj
If (Obj.hWnd = GetForegroundWindow) Then
If (InStr(1, Trim(LCase(Obj.FullName)), "iexplore.exe", vbTextCompare) > 0) Then
CurrentFocusWindow = Obj.LocationURL
If (CurrentFocusWindow <> LastFocusWindow) Then
UpdateBrowserObjects Obj
Exit For
End If
End If
End If
Next
'Clear the Obj
Set Obj = Nothing
(CurrentFocusWindow and LastFocusWindow are used to make sure the focus is changing to a different control, the CurrentFocusWindow is set to the obj.locationURL of the window with the current focus. While LastFocusWindow is set only when the app successfully changes to a browser)
Now, as you see if everything is right then I pass Obj through the sub UpdateBrowserObjects....
Public Sub UpdateBrowserObjects(Obj As InternetExplorer)
tmrCheckWindowFocus.Enabled = False
If (Obj.hWnd = GetForegroundWindow) Then
' Set the WebDoc conrol so we can control the page
' Set mhtmDoc3 = Nothing
' Set WebDoc = Nothing
Set mhtmDoc3 = Obj.document
Set WebDoc = mhtmDoc3
Set IEObj = Obj
LastFocusWindow = Obj.LocationURL
' Stop
Beep
Text2.Text = WebDoc.location
' Exit For
End If
'Next
Set Obj = Nothing
tmrCheckWindowFocus.Enabled = True
End Sub
(sorry for the commenting out of a lot of stuff, I have been trying to fix it)
(just ignore the Set mhtmDoc3 = Obj.document, Set WebDoc = mhtmDoc3, I am using that to give me access to the documents and events)
Now comes where it errors....
If I have two or more browsers open before the app starts I can switch between them fine. But if I close any of them, or open a new one I get an
Automation Error at
Set IEObj = Obj above in the UpdateBrowserObjects sub.
Why is this, any ideas
Thanks for any help
Mike