Solved

How can I give Internet Explorer focus?

Posted on 2006-04-09
5
195 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
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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
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…

867 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

20 Experts available now in Live!

Get 1:1 Help Now