Solved

Internet Explorer Always on top :  Vb6

Posted on 2004-09-25
7
1,615 Views
Last Modified: 2011-10-03
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
Comment
Question by:Jimmyx1000
  • 4
  • 2
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12153519
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
 

Author Comment

by:Jimmyx1000
ID: 12153627
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
 
LVL 14

Expert Comment

by:aelatik
ID: 12153631
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 14

Expert Comment

by:aelatik
ID: 12153632
It also requires a reference to microsoft internet controls !
0
 
LVL 14

Expert Comment

by:aelatik
ID: 12153633
BTW, it will also bring the window to foreground with ( IE.visible=true )
0
 
LVL 14

Expert Comment

by:aelatik
ID: 12153636
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12153655
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question