| VB6: Taming the Wild New IE Object | |||||||||
| Opening multiple IE objects in VB6 can be tricky! | |||||||||
Opening a new instance of IE in VB 6 can involve subtle problems. You can open an instance of IE in VB 6 with code that looks like this. (Remember to reference Microsoft Internet Controls in your project.) Simple, No? But suppose you decide that you need to create a new IE window and also to make sure the second web page has completely loaded before continuing. You would need to do this if, for example, there is some critical data on the web page that you need to use later in the program.
One way that you might decide to do this is to use the Flags parameter that you can pass to IE to create the new window. You might, for example, try this code: But ... what is that extra window with nothing in it? What's going on here? Here's what is happening. A "1" in the flag passed to the Navigate2 method tells IE to open a new window. What the Microsoft documentation doesn't explain very well (if at all) is that the ie object in your program refers to the NEW window after that and not the old one (which is still open). Furthermore, IE simply opens an "old" IE window with no content if one isn't available. And since the object refers to the "old" IE window rather than the new one and that one never has a URL address, the ReadyState is always 0. The correct way to code "most" of the requirements above is as follows: Private Sub Command1_Click()
' Create the IE window
Set ie1 = CreateObject("InternetExplorer.Application")
ie1.Visible = True
ie1.Navigate2 "http://www.about.com"
' Create the second IE window
ie1.Navigate2 "http://visualbasic.about.com", 1
' Wait for the second one to complete
Dim i As Integer
i = 1
While ie1.ReadyState < READYSTATE_COMPLETE
Sleep 100
Debug.Print "ie busy " & CStr(i * 0.1) & _
" seconds with ReadyState " & ie1.ReadyState
i = i + 1
Wend
End Sub
Why only "most" of the requirements? Why not "all"? Because the ReadyState of the first window is being tested, not the second. In fact, you might want to play around with this code a bit and see if you can access the second object at all. My bet is that you can't. .... Unless you use the technique described next! There are a number of key things that need to be changed to make this work.
When you do all of these things, you will have a project that looks like this one: Public WithEvents ie1 As InternetExplorer
Public ie2 As InternetExplorer
Private Declare Sub Sleep Lib "kernel32" ( _
ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Set ie1 = CreateObject("InternetExplorer.Application")
ie1.Visible = True
ie1.Navigate2 "http://www.yahoo.com"
While ie1.ReadyState < READYSTATE_COMPLETE
Sleep 100
Debug.Print "ie1 busy"
Wend
Debug.Print ie1.LocationURL & "AA"
ie1.Navigate2 "http://www.google.com", 1
While ie2.ReadyState < READYSTATE_COMPLETE
Sleep 100
Debug.Print "ie2 busy"
Wend
Debug.Print ie2.LocationURL & "BB"
End Sub
Private Sub ie1_NewWindow2(ppDisp As Object, Cancel As Boolean)
Set ie2 = CreateObject("InternetExplorer.Application")
Set ppDisp = ie2.Application
Debug.Print "NewWindow2"
End Sub
|
|||||||||

