Solved

How can I give Internet Explorer focus?

Posted on 2006-04-09
5
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:BobLamberson
ID: 16413850
JohnDoeSr,
<your IE object>.SetFocus

Bob
0
 

Author Comment

by:JohnDoeSr
ID: 16413868
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 86

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 16413900
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
ID: 16413916
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
ID: 16413937
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

751 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