Help with IE window name

Beastr0

New Member
Joined
Dec 28, 2004
Messages
22
I have the following code that was supposed to look for an Explorer window and if one was available it would select it and use that window instead of using another one... thre is only one instance that I wanted the script to go to the next IE window... I was using the ie.hwnd as the reference but didn't know this number always changed... basically,
if the window has the Name of "Unit Directory - Microsoft Internet Explorer" it is to look for the next available IE window... how would I go about this either with what I have below or possibly some easier way..

Code:
Sub UseExplorer()
Dim sw As Object, ie As Object
Set sw = CreateObject("Shell.Application").Windows
Dim FoundIE As Integer
Dim WS As Long

FoundIE = 0
For Each ie In sw
    If FoundIE = 1 Then
    ie.Navigate Site
    Exit Sub
    Else
    If InStr(UCase(ie.FullName), "\IEXPLORE.EXE") <> 0 Then
         FoundIE = FoundIE + 1
         If ie.hwnd = 3540814 Then
            FoundIE = 0
         Else
            ShowWindow ie.hwnd, 4
            SetForegroundWindow ie.hwnd
            ie.Navigate Site
         End If
    End If
    End If
Next
If FoundIE = 0 Then
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.Navigate Site
End If
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

hatman

Well-known Member
Joined
Apr 8, 2005
Messages
2,664
something more like this, perhaps?

Code:
    Private Declare Function GetWindowText _
    Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, _
    ByVal lpString As String, _
    ByVal cch As Long) As Long
    
    Private Declare Function GetWindowTextLength _
    Lib "user32" Alias "GetWindowTextLengthA" _
    (ByVal hwnd As Long) 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
    
    'Constants for ShowWindow()
    Const SW_HIDE As Long = 0
    Const SW_NORMAL As Long = 1
    Const SW_SHOWMINIMIZED As Long = 2
    Const SW_SHOWMAXIMIZED As Long = 3
    Const SW_SHOWNOACTIVATE As Long = 4
    Const SW_SHOW As Long = 5
    Const SW_MINIMIZE As Long = 6
    Const SW_SHOWMINNOACTIVE As Long = 7
    Const SW_SHOWNA As Long = 8
    Const SW_RESTORE As Long = 9
    Const SW_SHOWDEFAULT  As Long = 10



Sub stuff()

    Dim ieWin As Long
    Dim txt As String
    Dim mIE As InternetExplorer
    Dim SWs As New SHDocVw.ShellWindows
    Dim hwndTask As Long
    Dim winText As String
    
    ieWin = 0

    txt = "Unit Directory"
    
    site = "http:\\www.google.com"
    
    For Each mIE In SWs
    
        hwndTask = mIE.hwnd
        
        winText = String(GetWindowTextLength(hwndTask) + 1, Chr$(0))
        GetWindowText hwndTask, winText, Len(winText)
        
        If InStr(1, winText, txt, vbTextCompare) = 0 Then
        
            ieWin = hwndTask
            
            Exit For
            
        End If
        
    Next mIE
    
    If ieWin = 0 Then
    
        Set mIE = New InternetExplorer
        ieWin = mIE.hwnd
        
    End If
    
    mIE.Visible = True
    mIE.Navigate site
        
    SetForegroundWindow ieWin
    
    'hmm, seems redundant, but you had it in your code...
    ShowWindow ieWin, SW_SHOWNOACTIVATE
    
    Set mIE = Nothing

End Sub
 

Forum statistics

Threads
1,136,519
Messages
5,676,333
Members
419,619
Latest member
jalme

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top