Hi,
I need to find a specific tab in an open instance in Internet Explorer. I found a macro to bring Internet Explorer to the front, but then I need to find a specific tab and then activate it.
I have found macros that can verify that the tab is open, but I haven't managed to merge any of those codes into the macro I have for bringing IE to the front.
This is the macro I have for bringing IE to the front:
(Found at "Happy Codings" site)
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
I have seen some codes that uses SendKeys, but I would prefer to avoid that. And instead somehow loop through the names of the tabs to locate the correct one.
I have also posted the question some time ago here:
http://www.vbaexpress.com/foru…ance-of-Internet-Explorer
Thank you,
Bundi