Does anybody know how to set focus onto an IE object with Visual Basic? I\'ve tried myieobject.SetFocus
, but the compiler errors with this statement.
I needed a spreadsheet of mine to "set focus" to Internet Explorer after performing a function so I didn't have to bother clicking on it. This is what I found to work:
Const myPageTitle As String = "Title of my webpage"
Const myPageURL As String = "http://www.mywebpage.com"
Dim myIE As SHDocVw.InternetExplorer
Dim myIE As InternetExplorer
Set myIE = GetOpenIEByTitle(myPageTitle, False)
myIE.visible = false
DoEvents
myIE.visible = true
'for some reason, making the page invisible then visible always ensures it pops up
Function GetOpenIEByTitle(i_Title As String, _
Optional ByVal i_ExactMatch As Boolean = True) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows
If i_ExactMatch = False Then i_Title = "*" & i_Title & "*"
'ignore errors when accessing the document property
On Error Resume Next
'loop over all Shell-Windows
For Each GetOpenIEByTitle In objShellWindows
'if the document is of type HTMLDocument, it is an IE window
If TypeName(GetOpenIEByTitle.document) = "HTMLDocument" Then
'check the title
If GetOpenIEByTitle.document.Title Like i_Title Then
'leave, we found the right window
Exit Function
End If
End If
Next
End Function
Try this:
'First, hide the object even if it's visible
myieobject.Visible = False
' Second, show the object to focusing
myieobject.Visible = True
set .Visible=True
- if you've lost the screen somewhere in popups, you would have to loop through window titles to activate a specific title.
Dim objShell As Shell
Dim objIndex As InternetExplorer
Set objShell = New Shell
For Each objIndex In objShell.Windows
If TypeName(objIndex.Document) = "HTMLDocument" Then
If InStr(objIndex.Document.Title, "Stack Overflow") > 0 Then
objIndex.Visible = True
Exit For
End If
End If
Next objIndex
Here's what you can do with the IE object: MSDN
Try this. Open Internet explorer by shell command , where you can define focus(this is focused and small windows) and after that catch that shell/explorer window and define it as Internet explorer object. Maybe there is better way than sleep to wait.
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub call_IE()
Dim IE As InternetExplorer
Dim htmldoc As HTMLDocument
Set IE = Open_Focused_explorer()
IE.Navigate "google.com"
Set htmldoc = IE.Document
End Sub
Function Open_Focused_explorer() As InternetExplorer
Dim shellWins As ShellWindows
'if windows are 64bit IE is on diferent location
#If Win64 Then
Shell "C:\Program Files (x86)\Internet Explorer\iexplore.exe", vbNormalFocus
#Else
Shell "C:\Program Files\Internet Explorer\iexplore.exe", vbNormalFocus
#End If
'wait until explorer is full loaded
Sleep 4000
On Error Resume Next
'create collection of all explorers
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
' Get last one
Set Open_Focused_explorer = shellWins.Item(shellWins.Count - 1)
End If
On Error GoTo 0
End Function