HELP NEEDED: Open an existing internet explorer window using vba macro

jv0531

New Member
Joined
Mar 20, 2014
Messages
3
Hi Everyone,

I've been using excel for quite sometime now but it's my first time to do macro scripting. I need help to have a code that allows an excel button to open an existing internet explorer. I found several codes but what it does is open a new window and it takes few minutes to load the page. I already have a particular website open and I just want to go back to that IE window after executing some excel task. It's like doing ALT+TAB from excel to ie. I already have some scripts on and wanted to add opening existing IE window in one button click. I wanted to just do record macro but it doesn't capture opening an internet explorer window. Hope someone can help. Thanks in advance!!! :)

Here's the script i've found but wont work for me since it will open a new window.


Sub LoadSite_Click()

Dim iExplorer
Set iExplorer = CreateObject("InternetExplorer.Application")
iExplorer.navigate "site url"
iExplorer.Visible = True

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Tried to use this code but didn't work. Need it badly..


Dim shellWin As ShellWindows Dim IE As InternetExplorer Set shellWin = New ShellWindows If shellWin.Count > 0 Then ' Get IE Set IE = shellWins.Item(0) Else ' Create IE Set IE = New InternetExplorer IE.Visible = True End If IE.Navigate "site name" Set shellWin = Nothing Set IE = Nothing</pre>
 
Upvote 0
Here is a function and test routine which looks for and uses an existing Internet Explorer window. If the window does not exist it opens a new IE window. This code uses early binding of the InternetExplorer object so you will need to set a reference to Microsoft Internet Controls in the Tools - References menu in the VBA editor.

Code:
Public Sub Test_IE_Window()

    Dim IE As SHDocVw.InternetExplorer
    Dim URL As String
    
    URL = "http://www.mrexcel.com"
    
    Set IE = Get_IE_Window2
    If IE Is Nothing Then
        Set IE = New SHDocVw.InternetExplorer
    End If
    
    With IE
        .Visible = True
        .Navigate URL
    End With

End Sub


Private Function Get_IE_Window2() As SHDocVw.InternetExplorer

    'Look for an IE browser window and, if found, return that browser as an InternetExplorer object.  Otherwise return Nothing

    Dim Shell As Object
    Dim IE As Object
    Dim i As Variant 'Must be a Variant to index Shell.Windows.Item() array
    
    Set Shell = CreateObject("Shell.Application")
    
    i = 0
    Set Get_IE_Window2 = Nothing
    While i < Shell.Windows.Count And Get_IE_Window2 Is Nothing
        Set IE = Shell.Windows.Item(i)
        If Not IE Is Nothing Then
            Debug.Print IE.LocationURL, IE.LocationName
            If TypeName(IE) = "IWebBrowser2" Then
                If TypeOf IE Is SHDocVw.InternetExplorer And IE.LocationURL <> "" And InStr(IE.LocationURL, "file://") <> 1 Then
                    Set Get_IE_Window2 = IE
                End If
            End If
        End If
        i = i + 1
    Wend
    
End Function
 
Upvote 0
I know this is an old question, but for anyone interested I have found a really neat way to open an already existing Internet Explorer Web Page. This one creates a form and lists all open webpages for the user to select from. Just remember you have to highlight the line you want selected. It will set the browser's document to a global variable called "myDoc" and set the webpage to "IE." You must have "Microsoft Internet Controls" and "Microsoft HTML Object Library" added to your module references. (Tools -> References)
Code:
Public myDoc As HTMLDocument
Public IE As Object

Sub getOpenBrowserCreateForm()
Dim myForm As Object
Dim NewFrame As MSForms.Frame
Dim NewButton As MSForms.CommandButton, newButton2 As MSForms.CommandButton
'Dim NewComboBox As MSForms.ComboBox
Dim NewListBox As MSForms.ListBox
'Dim NewTextBox As MSForms.TextBox
'Dim NewLabel As MSForms.Label
'Dim NewOptionButton As MSForms.OptionButton
'Dim NewCheckBox As MSForms.CheckBox
Dim X As Integer
Dim Line As Integer
'This is to stop screen flashing while creating form
Application.VBE.MainWindow.Visible = False
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
'Create the User Form
With myForm
    .Properties("Caption") = "Select Open Web Site"
    .Properties("Width") = 326
    .Properties("Height") = 280
End With
'Create ListBox
Set NewListBox = myForm.designer.Controls.Add("Forms.listbox.1")
With NewListBox
    .Name = "ListBox1"
    .Top = 12
    .Left = 12
    .Width = 297
    .Height = 207.8
    .Font.Size = 9
    .Font.Name = "Tahoma"
    .BorderStyle = fmBorderStyleOpaque
    .SpecialEffect = fmSpecialEffectSunken
End With
'Create CommandButton1 Create
Set NewButton = myForm.designer.Controls.Add("Forms.commandbutton.1")
With NewButton
    .Name = "CommandButton1"
    .Caption = "Select"
    .Accelerator = "M"
    .Top = 228
    .Left = 234
    .Width = 72
    .Height = 24
    .Font.Size = 9
    .Font.Name = "Tahoma"
    .BackStyle = fmBackStyleOpaque
End With
'Create CommandButton2 Create
Set NewButton = myForm.designer.Controls.Add("Forms.commandbutton.1")
With NewButton
    .Name = "CommandButton2"
    .Caption = "Cancel"
    .Accelerator = "M"
    .Top = 228
    .Left = 144
    .Width = 72
    .Height = 24
    .Font.Size = 9
    .Font.Name = "Tahoma"
    .BackStyle = fmBackStyleOpaque
End With
'add code for form module
myForm.codemodule.insertlines 1, "Private Sub CommandButton1_Click()"
myForm.codemodule.insertlines 2, "Dim urlLocation As String"
myForm.codemodule.insertlines 3, ""
myForm.codemodule.insertlines 4, "''////////////////////////////////////////////////////////////////////"
myForm.codemodule.insertlines 5, "''  This part gets all open web pages qand displays them on the form for user to choose"
myForm.codemodule.insertlines 6, "''"
myForm.codemodule.insertlines 7, "    Set objIterator = CreateObject(" & Chr(34) & "Shell.Application" & Chr(34) & ")"
myForm.codemodule.insertlines 8, "    For X = 0 To objIterator.Windows.Count"
myForm.codemodule.insertlines 9, "        On Error Resume Next"
myForm.codemodule.insertlines 10, "        current_title = objIterator.Windows(X).Document.Title"
myForm.codemodule.insertlines 11, "        current_url = objIterator.Windows(X).******************"
myForm.codemodule.insertlines 12, "    "
myForm.codemodule.insertlines 13, "        If current_title = ListBox1.Value Then 'is this my webpage?"
myForm.codemodule.insertlines 14, "        
myForm.codemodule.insertlines 15, "            Set IE = objIterator.Windows(X)"
myForm.codemodule.insertlines 16, "            MsgBox " & Chr(34) & "IE was properly set" & Chr(34) & ""
myForm.codemodule.insertlines 17, "            "
myForm.codemodule.insertlines 18, "             Boolean_indicator = True"
myForm.codemodule.insertlines 19, "            Exit For"
myForm.codemodule.insertlines 20, "        End If"
myForm.codemodule.insertlines 21, "    Next"
myForm.codemodule.insertlines 22, "    Set objIterator = Nothing"
myForm.codemodule.insertlines 23, "    Set myDoc = IE.Document"
myForm.codemodule.insertlines 24, "Return"
myForm.codemodule.insertlines 25, "Unload Me"
myForm.codemodule.insertlines 26, ""
myForm.codemodule.insertlines 27, "End Sub"
myForm.codemodule.insertlines 28, ""
myForm.codemodule.insertlines 29, ""
myForm.codemodule.insertlines 30, "Private Sub CommandButton2_Click()"
myForm.codemodule.insertlines 31, " Unload Me"
myForm.codemodule.insertlines 32, "End Sub"
myForm.codemodule.insertlines 33, ""
myForm.codemodule.insertlines 34, ""
myForm.codemodule.insertlines 35, "Private Sub UserForm_Activate()"
myForm.codemodule.insertlines 36, "    Dim myArray1() As String, tempNumb As Integer"
myForm.codemodule.insertlines 37, "    "
myForm.codemodule.insertlines 38, "    "
myForm.codemodule.insertlines 39, "    i = 2"
myForm.codemodule.insertlines 40, "    tempNumb = 1"
myForm.codemodule.insertlines 41, "    "
myForm.codemodule.insertlines 42, "    ReDim myArray1(1 To 1)"
myForm.codemodule.insertlines 43, "   "
myForm.codemodule.insertlines 44, "    Set objShell = CreateObject(" & Chr(34) & "Shell.Application" & Chr(34) & ")"
myForm.codemodule.insertlines 45, "    Set objAllWindows = objShell.Windows"
myForm.codemodule.insertlines 46, "    "
myForm.codemodule.insertlines 47, "    "
myForm.codemodule.insertlines 48, "    For Each ow In objAllWindows"
myForm.codemodule.insertlines 49, "        If (InStr(1, ow," & Chr(34) & "Internet Explorer" & Chr(34) & ", vbTextCompare)) Then"
myForm.codemodule.insertlines 50, "            myArray1(tempNumb) = ow.Document.Title"
myForm.codemodule.insertlines 51, "            tempNumb = tempNumb + 1"
myForm.codemodule.insertlines 52, "            If Not ow.Document.Title = " & Chr(34) & "" & Chr(34) & " Then"
myForm.codemodule.insertlines 53, "                ReDim Preserve myArray1(1 To tempNumb)"
myForm.codemodule.insertlines 54, "            Else"
myForm.codemodule.insertlines 55, "                Exit For"
myForm.codemodule.insertlines 56, "            End If"
myForm.codemodule.insertlines 57, "        End If"
myForm.codemodule.insertlines 58, "    Next"
myForm.codemodule.insertlines 59, "     "
myForm.codemodule.insertlines 60, "    Me.ListBox1.List = myArray1"
myForm.codemodule.insertlines 61, "End Sub"
myForm.codemodule.insertlines 62, ""
'Show the form
VBA.UserForms.Add(myForm.Name).Show
'Delete the form (Optional)
Application.VBE.MainWindow.Visible = True
ThisWorkbook.VBProject.VBComponents.Remove myForm
'   IE is now set to the user's choice and you can add code here to interact with it
'   myDoc is now set to IE.Document also
'
'
'
End Sub
 
Upvote 0
Here is a function and test routine which looks for and uses an existing Internet Explorer window. If the window does not exist it opens a new IE window. This code uses early binding of the InternetExplorer object so you will need to set a reference to Microsoft Internet Controls in the Tools - References menu in the VBA editor.

Code:
Public Sub Test_IE_Window()

    Dim IE As SHDocVw.InternetExplorer
    Dim URL As String
   
    URL = "http://www.mrexcel.com"
   
    Set IE = Get_IE_Window2
    If IE Is Nothing Then
        Set IE = New SHDocVw.InternetExplorer
    End If
   
    With IE
        .Visible = True
        .Navigate URL
    End With

End Sub


Private Function Get_IE_Window2() As SHDocVw.InternetExplorer

    'Look for an IE browser window and, if found, return that browser as an InternetExplorer object.  Otherwise return Nothing

    Dim Shell As Object
    Dim IE As Object
    Dim i As Variant 'Must be a Variant to index Shell.Windows.Item() array
   
    Set Shell = CreateObject("Shell.Application")
   
    i = 0
    Set Get_IE_Window2 = Nothing
    While i < Shell.Windows.Count And Get_IE_Window2 Is Nothing
        Set IE = Shell.Windows.Item(i)
        If Not IE Is Nothing Then
            Debug.Print IE.LocationURL, IE.LocationName
            If TypeName(IE) = "IWebBrowser2" Then
                If TypeOf IE Is SHDocVw.InternetExplorer And IE.LocationURL <> "" And InStr(IE.LocationURL, "file://") <> 1 Then
                    Set Get_IE_Window2 = IE
                End If
            End If
        End If
        i = i + 1
    Wend
   
End Function
Hi John_w,
Is it possible to open the website in new tab instead of the 1st tab in IE ?
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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