Solved

How can I give Internet Explorer focus?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

839 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