PDA

View Full Version : Select specific tab in already open instance of Internet Explorer



Bundi
05-16-2017, 01:18 AM
Hi,

I have been trying to find a macro to select a specific tab in a running instance of Internet Explorer.
I have found a code to activate Internet Explorer and bring it to the front, but after this I need to "loop" through the tabs and then have the correct tab activated.
I have seen codes for looping through tabs but I haven't managed to combine the codes, idea is to find the tab if the title of the tab contains a string "like" so and so.

Thankful for suggestions on how I can activate the tab once found.

Best,
Bundi

The code I found for activating another application (with changes for IE instead of Excel as in the original)
Creds to "Happy Codings" site for that piece of code


Option Explicit




Private Type POINTAPI
x As Long
y As Long
End Type


Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type


Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Const SW_MINIMIZE = 6, SW_NORMAL = 1, SW_MAXIMIZE = 3, SW_RESTORE = 9




'-------METHOD 1--------------


'Purpose : Brings the specified application to foreground and alters it's window state.
'Inputs : [sFormCaption] The caption of the dialog to bring to the foreground.
' OR
' [lHwnd] The window handle of the dialog to bring to the foreground.
' [lWindowState] Specifies the windowstate of the application (defaults to normal)
'Outputs : Returns True on success
'Notes : Specify either the application caption or it's windows handle
'Revisions :


Function AppToForeground(Optional sFormCaption As String, Optional lHwnd As Long, Optional lWindowState As Long = SW_NORMAL) As Boolean
Dim tWinPlace As WINDOWPLACEMENT


If lHwnd = 0 Then
lHwnd = DialogGetHwnd(sFormCaption)
End If
If lHwnd Then
tWinPlace.Length = Len(tWinPlace)
'Get the windows current placement
Call GetWindowPlacement(lHwnd, tWinPlace)
'Set the windows placement
tWinPlace.showCmd = lWindowState
'Change window state
Call SetWindowPlacement(lHwnd, tWinPlace)
'Bring to foreground
AppToForeground = SetForegroundWindow(lHwnd)
End If
End Function




'-------METHOD 2--------------


'Purpose : Brings the specified application to foreground and alters it's window state.
'Inputs : [sApplicationCaption] The application caption.
' [lHwnd] The handle to the application.
' [lWindowState] Specifies the windowstate of the application (defaults to normal)
'Outputs : Returns True on success
'Notes : Specify either the application caption or it's windows handle
'Revisions :


Function AppToForeground2(Optional sApplicationCaption As String, Optional lHwnd As Long, Optional lWindowState As Long = SW_NORMAL) As Boolean



If lHwnd = 0 Then
lHwnd = DialogGetHwnd(sApplicationCaption)
End If
If lHwnd Then
'Alter window state
AppToForeground2 = ShowWindow(lHwnd, lWindowState)
'Bring to foreground
AppToForeground2 = SetForegroundWindow(lHwnd)
End If
End Function




'Demonstration routine, brings Excel to foreground and maximises it
Sub Test()
Dim lHwnd As Long

'Get IE's handle
lHwnd = DialogGetHwnd(, "IEFrame")
If lHwnd Then
If AppToForeground(, lHwnd, SW_MAXIMIZE) Then
MsgBox "Maximized IE", vbSystemModal
Else
MsgBox "Failed to Maximised IE", vbSystemModal
End If
Else
MsgBox "Please open IE before trying this demonstration"
End If
End Sub




'Purpose : Returns the Windows Handle of a Dialog based on its caption.
'Inputs : [sDialogCaption] The dialog caption.
' [sClassName] The class name of the dialog. If unknown, do not specify
' this parameter.
'Outputs : The Dialogs Window Handle
'Notes : Specify either a dialog caption or class name or both.
'Revisions :


Function DialogGetHwnd(Optional ByVal sDialogCaption As String = vbNullString, Optional sClassName As String = vbNullString) As Long
On Error Resume Next
DialogGetHwnd = FindWindowA(sClassName, sDialogCaption)
On Error GoTo 0
End Function