Controlling Internet Explorer local intranet using Excel VBA
Asked Answered
C

11

19

I am using Excel VBA code to click a button on our website. I know this isn't the best of things to be doing, but it is the least objectionable option available to me.

I can using this code, successfully load imdb.com, google, etc. But when I load our local site, I lose control of the ie object, I can't check readyState, I can't Quit.

Here is the error I get.

Run-time error '-2147023179 (800706b5)':
Automation error
The interface is unknown

Every so often I instead get this message:

Run-time error '-2147417848 (80010108)':
Automation error
The object invoked has disconnected from its clients.

Clicking Debug indicates the ie.readyState, I commented that out and then it points to ie.Quit

Sub dothestuff()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer
    ie.Visible = True

    ie.Navigate "http://www.google.com/"
    anerror = webload(ie)

    ie.Quit
    Set ie = Nothing
End Sub

Function webload(ie)
    Do Until ie.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
End Function
Cutter answered 18/10, 2012 at 22:59 Comment(1)
For everyone else struggling with those kind of issues, I have found a solution and compiled a summary with sample instructions here. It does not rely on using a fixed IP, nor changing the security settings and neither do you need to include references to anything. That being said, thanks a lot for all your contributions and valuable suggestions - this seems to be a bug in how event handling with IE works in relation to security zones.Sipple
D
32

Here is a quick and easy solution for this issue:

Instead of:

set IE = createobject("internetexplorer.application")

Use:

Set IE = New InternetExplorerMedium

No need to tweak the IE settings

Dorsal answered 7/10, 2013 at 9:33 Comment(1)
InternetExplorerMedium requires a reference to Microsoft Internet ControlsMehitable
T
19

Here's what's happening. When your browser internally "jumps" to a different security zone - from say a local address to an inTRAnet address or to an inTERnet address - IE closes/drops the current process of IE and opens another one with tighter security. The old and new IE processes are IE child processes to the parent IE task so you don't see it happen either by watching the browser or watching the process in Task Manager. If you use Process Explorer (free from Microsoft), you can see this happen.

What you need to do in this type of environment is use Anup Upadhyay's solution above. His code snippet looks at all IE tasks (parent and child doesn't make a difference) and finds the new IE process that is pointing to the web address that the original process was given. As long as you don't have multiple browsers open to the same address, it will find the new process reliably and carry on like you would expect it to.

Note: You might also need to use the InternetExplorerMedium object as opposed to the InternetExplorer object. It is also better at "holding on" to a session.

Here's my version which is almost the same thing as Anup's.

References for MS Office VBA:

  • Microsoft Internet Controls

  • Microsoft Shell Controls and Automation

n

Dim IE As InternetExplorerMedium ' This object (the "medium" variety as opposed to "InternetExplorer") is necessary in our security climate
Dim targetURL As String
Dim webContent As String
Dim sh
Dim eachIE

targetURL = "[your URL here]"
Set IE = New InternetExplorerMedium
IE.Visible = False ' Set to true to watch what's happening
IE.Navigate targetURL

While IE.Busy
  DoEvents
  Wend

Do
  Set sh = New Shell32.Shell
  For Each eachIE In sh.Windows
    If InStr(1, eachIE.LocationURL, targetURL) Then
      Set IE = eachIE
      'IE.Visible = False  'This is here because in some environments, the new process defaults to Visible.  
      Exit Do
      End If
    Next eachIE
  Loop
Set eachIE = Nothing
Set sh = Nothing

While IE.Busy  ' The new process may still be busy even after you find it
  DoEvents
  Wend
Trimetric answered 5/1, 2015 at 17:39 Comment(0)
T
10

Try this one:

Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
Thing answered 15/7, 2013 at 19:12 Comment(1)
Clever, this is simply the class id of InternetExplorerMedium, which has no progid unless you create one, for example InternetExplorer.ApplicationMedium:[HKEY_CLASSES_ROOT\InternetExplorer.ApplicationMedium] [HKEY_CLASSES_ROOT\InternetExplorer.ApplicationMedium\CLSID] @="{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}"See blogs.msdn.microsoft.com/ieinternals/2011/08/03/…Ricoriki
C
7

We had this problem using IE9. It arises because of security settings. There is a setting called "Enable Protected Mode" on the "Security" tab of the "Internet Options" dialog box (tools...internet options). Each "zone" can have a different setting for "enable protected mode" We unchecked "enable protected mode" and this solved our problem. I think what is happening is that when switch to a zone that is in protected mode, IE starts a new process to handle traffic in that zone (maybe killing the old process). When this happens your IE object variable in VBA gets disconnected from Internet Explorer.

Custard answered 28/3, 2013 at 16:42 Comment(2)
Thank you! I've been working on this, trying everything under the sun in VBA! Many googles later, I came to this, and it solved the problem. I was getting various runtime errors all at the same line. It was driving me nuts!Popover
So many issues... so many resolutions... this one worked for me.Calmative
C
4

Alright, just found a solution, decided out of desperation to try loading 127.0.0.1 instead of localhost, sure enough, no problems, so resolved the ip address of the local intranet server, and now I am good to go. I don't really understand why, but this has solved my issue.

Cutter answered 19/10, 2012 at 16:35 Comment(4)
just curious, did you use "http : //localhost/" (without the spaces) or just localhost in the URL?Kalgoorlie
I used http : //localhost/ (without spaces) The way I got it working was using http : //127.0.0.1/ (without spaces)Cutter
thanks for the info. not sure why it's working like that, but not like the other way...but it's good to know for any future coding I may do :)Kalgoorlie
Yeah, I truly don't understand why either, just happy to have a working solution. Would love if anyone else could explain the reason for this behavior.Cutter
J
4
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate "C:\abc.html"

'We are getting error here , I have found a small tricky alternate solution for this as below 'Take reference for Microsoft Shell Controls And Automation 'Use below code to reassign IE object for the lost connection issue

Dim sh
Dim eachIE
Do

    Set sh = New Shell32.Shell
    For Each eachIE In sh.Windows
        ' Check if this is the desired URL

' Here you can use your condition except .html
' If you want to use your URL , then put the URL below in the code for condition check.
    ' This code will reassign your IE object with the same reference for navigation and your issue will resolve.
' 
        If InStr(1, eachIE.locationurl, ".html") Then 
        Set IE = eachIE
        Exit Do
        End If
    Next eachIE
Loop
Jowers answered 5/11, 2013 at 5:44 Comment(0)
M
2

For me, the following worked:

  1. Open Internet explorer
  2. Go to tools
  3. Goto Internet options
  4. Switch to Security tab
  5. Click Trusted sites
  6. Click sites
  7. You will see the site url, select it
  8. Click Remove
  9. Click close, apply and Ok

Ps. I tested these steps on IE 8 browser.

Mancino answered 30/3, 2015 at 15:14 Comment(1)
@kjack Glad to know this!Mancino
K
1

Maybe you're having a problem with binding? That's a strange error to get for this.

Try this code (adapted from http://www.excely.com/excel-vba/ie-automation.shtml). Hopefully it helps:

Option Explicit

' lasts for the life of Excel being open
Dim ie As Object

Sub dothestuff()
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate "http://www.google.com/"

    Do While ie.Busy
        DoEvents
    Loop

    ie.Quit
    Set ie = Nothing
End Sub
Kalgoorlie answered 19/10, 2012 at 13:55 Comment(3)
Unfortunately no, still getting the same message with that change, still works with public domains like google.com, but localhost doesn't work.Cutter
is the intranet site on your machine or a server on your intranet?Kalgoorlie
server on the intranet, I just tested the same with localhost server hosted on this machine, same exact errors.Cutter
Q
1

I am having the exact same issue working with a website that is inside our company intranet and is also included in my trusted sites. Disabling protected mode allows me to check ReadyState, but only if I disable for the internet zone in addition to intranet and trusted sites.

Why would enabling protected mode for the internet zone cause the automation error when checking ReadyState on a trusted site? Is there a way around this without disabling protected mode for the internet zone?

Quiche answered 17/7, 2013 at 13:28 Comment(0)
S
1

Another solution..

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate ("http://URLOnMyIntranet/site.html")
IE.Visible = True

Set IE = nothing
Set objShellApp = CreateObject("Shell.Application")
For Each objWindow In objShellApp.Windows
    Debug.Print objWindow.LocationName
     If LCase(objWindow.LocationName) = LCase("Your windows name, use debug to find out") Then
            Set IE = objWindow
     End If
Next

Note, using InternetExplorerMedium (see other answers) usually all you need, FYI also you could use Url property obj.LocationURL instead of window name which would be more accurate. See other answers.

Sonata answered 26/2, 2015 at 0:52 Comment(0)
F
0
Dim IE As InternetExplorer
Set IE = New InternetExplorerMedium
IE.Navigate "yousite.org"
IE.Visible = True

Do While IE.Busy
    DoEvents
Loop    

Set HTML = IE.document
Do
var8 = HTML.DocumentElement.innerHTML
v3 = var8
v4 = InStrRev(v3, "the class or ID you are looking for when the _    
page final loads", -1)
Loop While v4 = 0

Do While IE.Busy
    DoEvents
Loop 

'Your in the new page look for elements and click rinse and repeat
'the Do loop. This method works with my intranet corporate site
Finagle answered 21/12, 2019 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.