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!
 
... couldn't make it work.
Is there anything else to do? or do you think I'm doing something wrong?
I can't find where the issue is.

If I run the sub it gives me the negative MsgBox

If I try running the function it says :

IE window with this Title is not found:
""
Looks like you mixed part of the code from post #75 with some previous code because the above Red message is not present in the code of post #75.
Open manually in IE these links:
http://www.mrexcel.com/
https://www.mrexcel.com/forum/faq.php
and some other web pages.

Add the new Module to the VBA project and put the below code to it, which is the same as in the post #75 but with HtmlText = "wp-image-763" for url = http://www.mrexcel.com/
Then run the Main()
Rich (BB code):
Sub Main()
  Dim IE As Object, HtmlText As String
  HtmlText = "wp-image-763"   ' <-- this string is present in the html code of main mrexcel.com web page
  Set IE = GetIeByHtmlText(HtmlText)
  If IE Is Nothing Then
    MsgBox "HtmlText is not found in any open IE instances"
  Else
     MsgBox "HtmlText is found in the open IE instance with URL:" & vbLf & IE.LocationURL
  End If
End Sub
 
Function GetIeByHtmlText(HtmlText As String) As Object
' ZVI: 2017-03-18 https://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window-2.html#post4781479
  Dim w As Object
  Dim i As Long
  For Each w In CreateObject("Shell.Application").Windows
    If w.Name = "Windows Internet Explorer" Then
      i = InStr(1, w.Document.Body.innerHTML, HtmlText, vbTextCompare)
      If i > 0 Then
        Set GetIeByHtmlText = w
        Exit For
      End If
    End If
  Next
End Function
Message will say that already open instance of IE is found in the main mrexcel web page
 
Last edited:
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,

Is there a vba code where:
1. it open the internet explorer first
2. type something in search column (eg.Happy Birthday). We get the images
3. select the image and by right clicking on the image to save, it should save in the folder.

Could any one please help me with this.

Thank You
 
Upvote 0
I've tried Your original code and it's doing nothing. I use 64-bit Excel (the reference to MS Internet Controls is set). Can You hehlp me out?

Hi & Welcome to the Board!
Try this:
Rich (BB code):

' ZVI:2011-05-30 http://www.mrexcel.com/forum/showthread.php?t=553580
' Reference required: Tools - References - Microsoft Internet Controls
Sub GetIE()
  
  Dim shellWins As ShellWindows
  Dim IE As InternetExplorer

  Set shellWins = New ShellWindows

  If shellWins.Count > 0 Then
    ' Get IE
    Set IE = shellWins.Item(0)
  Else
    ' Create IE
    Set IE = New InternetExplorer
    IE.Visible = True
  End If

  IE.Navigate "http://support.microsoft.com/kb/q176792/"

  Set shellWins = Nothing
  Set IE = Nothing

End Sub
Regards,
 
Upvote 0
I've tried Your original code and it's doing nothing. I use 64-bit Excel (the reference to MS Internet Controls is set). Can You hehlp me out?
May be that link is broken now, try this:
Rich (BB code):
Sub GetIE_01()
 
  Dim shellWins As ShellWindows
  Dim IE As InternetExplorer
 
  Set shellWins = New ShellWindows
 
  If shellWins.Count > 0 Then
    ' Get IE
    Set IE = shellWins.Item(0)
  Else
    ' Create IE
    Set IE = New InternetExplorer
  End If
 
  IE.Navigate "http://www.mrexcel.com/forum/index.php"
 
  ' Activate IE
  IE.Visible = False
  IE.Visible = True
 
  Set shellWins = Nothing
  Set IE = Nothing
 
End Sub
 
Upvote 0
Hi & Welcome to the Board!
Try this code:
Rich (BB code):

Function NavigateTo(Link As String, Optional WaitSeconds = 5) As Long
' ZVI:2011-08-04 http://www.mrexcel.com/forum/showthread.php?t=553580
' Navigate (Internet)Explorer to the Link with [WaitSeconds=5] timeout.
' If (Internet)Explorer is already navigated to the Link then it's just activated,
' else the new (Internet)Explorer is navigated to the Link.
' Returns  error number (zero at success)
' Note: comment "Application.StatusBar" lines for VB & VBScript compatibility

  Dim i As Long, t As Single, Url As String, w As Object, wUrl As String
  
  ' Uniform Link string
  Url = Trim(Replace(Replace(Link, "%20", " "), "", "/"))
  ' Find "://" prefix
  i = InStr(Url, "://")
  If i > 1 And i < 7 Then Url = Mid(Url, i + 3)
  ' Delete "/" at the end
  If Right(Url, 1) = "/" Then Url = Left(Url, Len(Url) - 1)
    
  ' Seach Link in IE windows
  Application.StatusBar = "Finding link: " & Link & " ..."
  For Each w In CreateObject("Shell.Application").Windows
    ' Uniform LocationURL string
    wUrl = Trim(Replace(Replace(w.LocationURL, "%20", " "), "", "/"))
    ' Find "://" prefix
    i = InStr(wUrl, "://")
    If i > 1 And i < 7 Then wUrl = Mid(wUrl, i + 3)
    ' Del 3d "/" in prefix for local Link
    If Mid(wUrl, 1, 1) = "/" Then wUrl = Mid(wUrl, 2)
    ' Delete "/" at the end
    If Right(wUrl, 1) = "/" Then wUrl = Left(wUrl, Len(wUrl) - 1)
    If StrComp(Url, wUrl, 1) = 0 Then
      ' Link is found - activate it's IE window
      w.Visible = True
      Exit For
    Else
       wUrl = ""
    End If
  Next
  
  ' If Link is not found then create new IE and navigate to the Link
  On Error Resume Next
  If Len(wUrl) = 0 Then
    With CreateObject("InternetExplorer.Application")
      ' Disable pop-up msgs
      .Silent = True
      ' Navigate
      Application.StatusBar = "Navigating to: " & Link & " ..."
      .Navigate Link
      ' Charge the timeout
      t = Timer + WaitSeconds
      ' Wait for "IE is ready" state
      Application.StatusBar = "Waiting for IE's complete state..."
      While .readyState <> 4 And Timer < t: DoEvents: Wend
      ' Wait for "IE.Document is completely downloaded" state
      If Timer < t Then
        Application.StatusBar = "Waiting for Document's downloaded state..."
        While .Document Is Nothing And Timer < t: DoEvents: Wend
      Else
        Err.Raise vbObjectError + 513, , "Timeout happens: " & WaitSeconds & " seconds"
      End If
      ' Activate IE
      Application.StatusBar = False
      If Err Then .Quit Else .Visible = True
    End With
  End If
  
  ' Release the memory of object variable
  Set w = Nothing
  
  ' Return error number (zero if successful)
  NavigateTo = Err.Number
  
  ' Show error message
  If Err.Number <> 0 Then
    Application.StatusBar = "NavigateTo: " & Replace(Err.Description, vbLf, " - ")
    ' Uncomment the line below to show error message
    'MsgBox Err.Description, vbExclamation, "NavigateTo"
  End If
  
End Function

Example of the calling:
Rich (BB code):

Sub Test_NavigateTo()
  
  ' Define the test number
  Const TEST = 2
  
  Select Case TEST
    Case 1: NavigateTo "about:blank"
    Case 2: NavigateTo "www.mrexcel.com/forum/showthread.php?t=553580"
    Case 3: NavigateTo "http://www.google.com/"
    Case 4: NavigateTo "http://support.microsoft.com/kb/q176792/"
    Case 5: NavigateTo "C:\Temp"
  End Select
  
End Sub




This is wonderful, but there is something I can't figure out.

If the page already exists, how do you refresh it instead of just activating it?
 
Upvote 0
That was great Vladimir, so close to my requirement and to get an idea about the same.

I was looking to open a list of web links in IE and wait for 5 sec.
Close the active IE page
Load another web link from excel cell A1 (and wait for 5 sec)
and loop the process till last value in cell A1


I tried this code

Code:
Sub OpenLinks()
    Dim Cell As Range
    Set LinkRng = Range("A1").CurrentRegion.Columns(1)
    On Error Resume Next
    For Each Cell In LinkRng.Cells
        Cell.Hyperlinks(1).Follow
        Application.Wait (Now + TimeValue("00:00:05"))
        Application.Quit
        'IE.Quit
        Next
    On Error GoTo 0
End Sub

but it keep on opening all the web pages one after another, which is different from what I am looking for

Kindly see if it is possible
 
Upvote 0
Late binding version, reference to Microsoft Internet Controls is not required:
Rich (BB code):

Sub GetIE_LateBinding()
  
  Dim IE As Object
  
  With CreateObject("Shell.Application").Windows
    
    If .Count > 0 Then
      ' Get IE
      Set IE = .Item(0) ' or .Item(.Count - 1)
    Else
      ' Create IE
      Set IE = CreateObject("InternetExplorer.Application")
      IE.Visible = True
    End If
  
    IE.Navigate "http://support.microsoft.com/kb/q176792/"
  
    Set IE = Nothing
  
  End With
  
End Sub
P.S. Window of any MS Explorer can be catched, not only of MS Internet Explorer.


I tried to use this code and add "Do while IE.readystate = 4: do events:loop" after the navigate part, but for some reason it loops endlessly. I did that because i want a confirmation that the webpage has already load if ever i will add other codes
 
Upvote 0
Hi,

When i am using the below code i.e.

Dim shell As ShellWindows

i am getting the compile error (User defined type not defined). Please help.

Regards:
Aditya jain
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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