VBA Macro For Already Open IE Window

RL101

New Member
Joined
May 29, 2011
Messages
1
Hi all,

I'm looking to create a VBA macro which will be ran from Excel (2003 or 2010) & which works with Internet Explorer. I have added 'Microsoft Internet Controls' to VBA References, and have looked at plenty of VBA code on the forum which opens & then navigates to specific websites.

However, what I need is for this macro to affect an already open IE window.

Does anyone have an idea of the syntax / command to have excel "select" a specific IE window already open?

Many thanks!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Wanted to thank you for your post. It came in very helpful today.
Welcome to MrExcel, Skybound!
And thank you for the response. I also learn here a lot every time
:beerchug:
 
Upvote 0
Hi,

I have a similar problem and have tried using the code in this thread, but cannot get it to work. I

I want to switch (from my userform) to a specific open Internet Explorer window. I know the title of the window (like on this thread the title is "VBA Macro for already open IE Window... etc."). Can I use that combined with your code to always switch to this open window?

Thanks in advance.
 
Upvote 0
Hi,

I have a similar problem and have tried using the code in this thread, but cannot get it to work. I

I want to switch (from my userform) to a specific open Internet Explorer window. I know the title of the window (like on this thread the title is "VBA Macro for already open IE Window... etc."). Can I use that combined with your code to always switch to this open window?

Thanks in advance.
Hi and welcome to MrExcel!
Try this:
Rich (BB code):

' ZVI:2012-12-19 Activating of IE window by its title
' Arguments:
'   Title     -  title of the searchied IE window
'   [IsLike]  -  False/True = exact/partial searching, default is False
Sub ActivateIeByTitle(Title, Optional IsLike As Boolean)
  Dim w As Object
  For Each w In CreateObject("Shell.Application").Windows
    With w
      If .Name = "Windows Internet Explorer" Then
        If IsLike Then
          If InStr(1, .LocationName & " - " & .Name, Title, vbTextCompare) > 0 Then
            ' Partial title of window is found - activate IE window
            w.Visible = False
            w.Visible = True
            Exit For
          End If
        Else
          If StrComp(.LocationName & " - " & .Name, Title) = 0 Then
            ' Title of window is found - activate IE window
            w.Visible = False
            w.Visible = True
            Exit For
          End If
        End If
      End If
    End With
  Next
  Set w = Nothing
End Sub
 
Sub Test1()
  ' Activate IE's window by exact searching in titles
  ActivateIeByTitle "VBA Macro For Already Open IE Window#post3345848 - Windows Internet Explorer"
End Sub
 
Sub Test2()
  ' Activate IE's window by partial searching in titles
  ActivateIeByTitle "VBA Macro For Already Open IE Window", True
End Sub
Regards,
 
Last edited:
Upvote 0
Hello,
Like with every one else, I am a new member. But this thread seems to be the area I am looking for.

Here is what I have.

I have an IE window opened. And I know the name of the window.

I need the marco to be able to access that object, so I can do something like set a value within the IE window. The setting of this value will trigger an action on the page, like a script that does an alert or something. Can a combination of the previous macros do that?

Thank you in advance,
Eric
 
Upvote 0
I have an IE window opened. And I know the name of the window.
I need the marco to be able to access that object, so I can do something like set a value within the IE window. The setting of this value will trigger an action on the page, like a script that does an alert or something.
Hi Eric,
Welcome to MrExcel !
Function GetIeByTitle finds IE window by window title and returns IE object.
See example of the usage in Test1 subroutine.
Rich (BB code):

' ZVI:2013-03-19 Get IE object of already open window by its title
' Arguments:
'   Title     -  title of the searchied IE window
'   [IsLike]  -  False/True = exact/partial searching, default is False
'   [IsFocus] -  False/True = don't_activate/activate IE window, default is False
Function GetIeByTitle(Title, Optional IsLike As Boolean, Optional IsFocus As Boolean) As Object
  Dim w As Object
  For Each w In CreateObject("Shell.Application").Windows
    With w
      If .Name = "Windows Internet Explorer" Then
        If IsLike Then
          If InStr(1, .LocationName & " - " & .Name, Title, vbTextCompare) > 0 Then
            ' Partial title of window is found - activate IE window
            If IsFocus Then
              w.Visible = False
              w.Visible = True
            End If
            Set GetIeByTitle = w
            Exit For
          End If
        Else
          If StrComp(.LocationName & " - " & .Name, Title) = 0 Then
            ' Title of window is found - activate IE window
            If IsFocus Then
              w.Visible = False
              w.Visible = True
            End If
            Set GetIeByTitle = w
            Exit For
          End If
        End If
      End If
    End With
  Next
  Set w = Nothing
End Function
 
Sub Test1()
  Dim IE As Object
  Set IE = GetIeByTitle("VBA Macro For Already Open IE Window", True, True)
  IE.Navigate "http://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window-2.html#post3423470"
End Sub
Regards
 
Last edited:
Upvote 0
Another version of Test1 with error trapping:
Rich (BB code):
Sub Test1()
  Dim IE As Object
  Const Title = "VBA Macro For Already Open IE Window"
  Const URL = "http://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window-2.html#post3423470"
  Set IE = GetIeByTitle(Title, True, True)
  If IE Is Nothing Then
    MsgBox "Window is not found:" & vbLf & Title, vbCritical, "Error"
  Else
    IE.Navigate URL
  End If
End Sub
 
Upvote 0
I can see that I somehow completely forgot about this thread.

Thank you very much ZVI for your solution to my problem a while back. It is working perfectly.
 
Upvote 0
I can see that I somehow completely forgot about this thread.

Thank you very much ZVI for your solution to my problem a while back. It is working perfectly.
I'm happy to know it has helped :)
Thank you for the feedback!
 
Upvote 0
The responses in this thread have been great, so here is hoping that ZVI is still monitoring.

I used your late binding code as a basis to create this simple tester sub:
Code:
Sub ActivateIeByTitle(Title)
  Dim w As Object
  For Each w In CreateObject("Shell.Application").Windows
      If w.Name = "Windows Internet Explorer" Then
            Debug.Print w.locationname
            If w.locationname Like (Title & "*") Then
                w.Visible = False
                w.Visible = True
                Debug.Print "IE Page located"
                GoTo exit_sub
                
            End If
      End If
  Next
    Debug.Print "IE Page Not located"
exit_sub:
  Set w = Nothing
End Sub

The code runs fine on the two computers that I use (Windows XP with Excel 2007 SP3, and Windows 7 with Excel 2007 SP3), however it does not run on a colleagues comptuer who is also running Windows 7 with Excel 2007 SP3.


The Error "Method 'Windows' of object 'IShellDispatch5' Failed" is thrown on the line
Code:
For Each w In CreateObject("Shell.Application").Windows
.

Any suggestions on what might be the issue or how to resolve it?
 
Upvote 0

Forum statistics

Threads
1,224,844
Messages
6,181,294
Members
453,030
Latest member
PG626

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
Back
Top