VBA: Opening IE with VBA

Peter h

Active Member
Joined
Dec 8, 2015
Messages
417
Currently I have some code that opens IE manipulates it in the way that I want, and then closes it at the end of the sub. I have IE.Visible = False, so that it all happens in the background. I was just curious about something. Can I open the IE as invisible when the workbook opens, and then recall the same instance of that IE for each sub that uses it, and then have it close when I close the workbook? I'm thinking about trying this so that IE isn't constantly opening and closing every time I run a piece of the code. Will IE continue to work in the background? and how would I reference the IE with each sub? I'm sure I'd have to pass the variable between each sub, but I'm not extremely familiar with how to do stuff like that.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Simply declare IE as a global variable at the top of a module and define/assign it in the Workbook_Open event in the ThisWorkbook module, something like this:

ThisWorkbook module:

Code:
Private Sub Workbook_Open()
    Set IE = CreateObject("InternetExplorer.Application")
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not IE Is Nothing Then
        IE.Quit
        Set IE = Nothing
    End If
End Sub

Module1 (standard module):

Code:
Public IE As Object

Public Sub Open_Site() 'example procedure using the IE object
    With IE
        .Visible = False
        .navigate "https://www.mrexcel.com"
        While .busy Or .readystate <> 4: DoEvents: Wend
    End With
End Sub

Public Sub Display_URL() 'another procedure using the same IE object
    MsgBox IE.LocationURL
End Sub
 
Upvote 0
Simply declare IE as a global variable at the top of a module and define/assign it in the Workbook_Open event in the ThisWorkbook module, something like this:

ThisWorkbook module:

Code:
Private Sub Workbook_Open()
    Set IE = CreateObject("InternetExplorer.Application")
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not IE Is Nothing Then
        IE.Quit
        Set IE = Nothing
    End If
End Sub

Module1 (standard module):

Code:
Public IE As Object

Public Sub Open_Site() 'example procedure using the IE object
    With IE
        .Visible = False
        .navigate "https://www.mrexcel.com"
        While .busy Or .readystate <> 4: DoEvents: Wend
    End With
End Sub

Public Sub Display_URL() 'another procedure using the same IE object
    MsgBox IE.LocationURL
End Sub

Thank you very much. I apologize for not replying sooner. I will definitely give this try. I'll let you know how it goes.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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