• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1652
  • Last Modified:

Internet Explorer Always on top : Vb6

Hello Expert ,

I am using  the code below to open a web page , but sometimes the web page does not
always come to the front

How can i make sure the web page will always be displayed on top.


Private Sub Command1_Click()

Dim abc
 
     
    abc = "http://www.google.co.uk"
   
     
    Dim IE As New InternetExplorer   '  also add Reference to Microsoft Internet Controls
    IE.Navigate abc    ' Goto Web Address in variable i here
    IE.Visible = True

End Sub

'-----------------------------------------------------------------------------------------------------
Also in seperate code

How can i use an existing instance of IE  to display my web page rather than open
a new instance every time.

thanks experts








0
Jimmyx1000
Asked:
Jimmyx1000
  • 4
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here are both of the problems addressed:

Option Explicit

Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Private IE As InternetExplorer   '  also add Reference to Microsoft Internet Controls

Private Sub Form_Load()
    Set IE = New InternetExplorer
End Sub

Private Sub Command1_Click()
    Dim url As String
    url = "http://www.google.co.uk"
    IE.Navigate url    ' Goto Web Address in variable i here
    IE.Visible = True
    BringWindowToTop IE.hwnd
End Sub

Private Sub Command2_Click()
    Dim url As String
    url = "http://www.google.com"
    IE.Navigate url
    IE.Visible = True
    BringWindowToTop IE.hwnd
End Sub
0
 
Jimmyx1000Author Commented:
Hello Expert

Your 1st Code is good thankyou ( Command1 )

In Command2

Command2  works fine first time but then if you close  this IE instance

it gives an error

Automation error
the object invoked has disconnected from its clients




0
 
aelatikCommented:
This code will look for open IE sessions and use one if available, otherwise it will create a new instance :

Private Sub Form_Load()
        Dim DONE As Boolean
            DONE = False
        Dim IE As New InternetExplorer
        Dim WIN As New SHDocVw.ShellWindows
        ' Loop through all open Internet Explorer Sessions
        For Each IE In WIN
            If IE.LocationURL <> "" Then
                IE.Navigate "http://www.google.com"
                IE.Visible = True
                DONE = True
                Exit For
            End If
        Next
        ' If there is no instance, create a new one
        If DONE = False Then
            IE.Navigate "http://www.google.com"
            IE.Visible = True
        End If
End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aelatikCommented:
It also requires a reference to microsoft internet controls !
0
 
aelatikCommented:
BTW, it will also bring the window to foreground with ( IE.visible=true )
0
 
aelatikCommented:
Just put it in a command click event and try it out

Private Sub Command1_Click()
        Dim DONE As Boolean
            DONE = False
        Dim IE As New InternetExplorer
        Dim WIN As New SHDocVw.ShellWindows
        ' Loop through all open Internet Explorer Sessions
        For Each IE In WIN
            If IE.LocationURL <> "" Then
                IE.Navigate "http://www.google.com"
                IE.Visible = True
                DONE = True
                Exit For
            End If
        Next
        ' If there is no instance, create a new one
        If DONE = False Then
            IE.Navigate "http://www.google.com"
            IE.Visible = True
        End If
End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you only want to reuse the IE window you created then declare it WithEvents and then you can trap the OnQuit() event:

Option Explicit

Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Private WithEvents IE As InternetExplorer

Private Sub Command1_Click()
    openURL "http://www.google.co.uk"
End Sub

Private Sub Command2_Click()
    openURL "http://www.google.com"
End Sub

Private Sub openURL(ByVal url As String)
    If IE Is Nothing Then
        Set IE = New InternetExplorer
        While IE.Busy
            DoEvents
        Wend
    End If
    IE.Navigate url
    IE.Visible = True
    BringWindowToTop IE.hwnd
End Sub

Private Sub IE_OnQuit()
    Set IE = Nothing
End Sub
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now