Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I give Internet Explorer focus?

Posted on 2006-04-09
5
Medium Priority
?
210 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:Bob Lamberson
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 500 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:Bob Lamberson
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

715 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