Solved

How can I give Internet Explorer focus?

Posted on 2006-04-09
5
194 Views
Last Modified: 2010-08-05
I'm using Automation and controlling IE via a form window. I have a command button on my form that tells IE to go to a URL. I'd like to also set the focus to IE (IE on top) at this time as well. Any suggestions?
0
Comment
Question by:JohnDoeSr
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:BobLamberson
Comment Utility
JohnDoeSr,
<your IE object>.SetFocus

Bob
0
 

Author Comment

by:JohnDoeSr
Comment Utility
Hi Bob. When running with that code in I get a Run-time error '438'
"Object doesn't support this property or method"

I'm using IE as 'explorer' and entered the code in as 'explorer.setfocus'
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
How about...

' Project --> References --> "Microsoft Internet Controls"

Option Explicit

Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private WithEvents IE As InternetExplorer

Private Sub Command1_Click()
    If IE Is Nothing Then
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
    End If
    IE.Navigate Text1.Text
End Sub

Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    AppActivate GetWindowCaption(IE.hwnd)
End Sub

Private Function GetWindowCaption(ByVal windowHandle As Long) As String
    GetWindowCaption = String(GetWindowTextLength(windowHandle) + 1, Chr$(0))
    GetWindowText windowHandle, GetWindowCaption, Len(GetWindowCaption)
End Function

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

Expert Comment

by:BobLamberson
Comment Utility
JohnDoeSr,
> I'm using IE as 'explorer' and entered the code in as 'explorer.setfocus'

Try setting an object variable for the web browser object and use that variable in place of 'explorer'

Bob
0
 

Author Comment

by:JohnDoeSr
Comment Utility
Bob, normally I do use the webbrowser object but for this project I'd like to stick with IE

Idle_Mind, I didn't use your whole bit of code there, but reading through it gave me an idea of simply running "explorer.visible = true" again since it's first run on form.load and loses focus since I have to open a file on the form. That one liner brings the window to the front again and works like a charm.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now