VBA script fails on intranet - vba

I am currently using Office 2010, Visual Basic for Applications 7, and Internet Explorer 9.
The current issue I've been having is that using an IE object seems to fail on our intranet, and I was wondering if there was a fix for this.
Functioning code:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://www.google.com"
WaitUntilReady IE
'Application.Wait (Now + TimeValue("00:00:10"))
'IE.Visible = True
MsgBox (IE.Document.body.innerHTML)
Broken code:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://intranet/"
WaitUntilReady IE
'Application.Wait (Now + TimeValue("00:00:10"))
'IE.Visible = True
MsgBox (IE.Document.body.innerHTML)
Functioning code does exactly what it should. Makes an IE object which doesn't show up visibly on screen, and returns a message box with the HTML code contents of google.com
Broken code loads our intranet home page, in a VISIBLE IE window, and returns this error:
Run-time error '-2147417848 (80010108)'
Automation error
The object invoked has disconnected from its clients
I've read through Excel VBA Controlling IE local intranet which suggested using an IP address instead of intranet/ which doesn't work because the IP directs to a different splash screen.
I've also tried using
Set IE = New InternetExplorerMedium
But this A- didn't seem to work either, and B- would mean making sure all my fellow employees enable this Reference.
Also, I've tried loading google.com first, then having it navigate to intranet/, which also didn't help.
Anyone have any suggestions? From my understanding, loading intranet/ is causing IE to disconnect from Excel somehow?

SO! After several days of research and at a complete loss for what was going on, I stumbled on to someone talking about intranet compatibility mode in another article.
Force "Internet Explorer 8" browser mode in intranet
Things happened to click in my head when I read this, realizing that if IE loaded in compatibility, it would explain why the Excel VBA macro lost control of IE, as compatibility mode might be loading as a separate instance of IE.
Settings > Internet Options > Security > Local Intranet > Sites >
Disable "Auto detect intranet" and enable "Include all network paths (UNCs)" fixes the error I was having, perfectly.

'simply replace -seem to work
' Set ie = CreateObject("InternetExplorer.Application")
Set ie = New InternetExplorerMedium
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`enter code here`
also see this post

Related

Activate a Button on a WebPage using VBA on MsAccess

Hi I am trying to write a scraper to get data from this website (www.coned.com/tcisng) and dump data into my Ms Access Backend. When I navigate to it and enter in my credentials (username and password), it does not activate the "Log In" button which I want to click next as it is the next step on my scraper. Also this "Log In" button has no name or ID. How to activate and make it click using VBA in Ms Access 2016.
Updated Code Snippet -
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.Navigate "www.coned.com/tcisng"
While ie.Busy
DoEvents
Wend
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
ie.Document.all("LoginEmail").Value = "myemail"
ie.Document.all("LoginPassword").Value = "myPassword"
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
While ie.Busy
DoEvents
Wend
'ie.Document.all(48).Click
'ie.Document.querySelector("[type=submit]").Click
ie.Document.querySelector("button[title*='Log in']").Click
In the last 3 line I am trying to select the "Log In" button but it is disabled, also it has no name or ID, can you also help what exact code to replace in order to activate the button and click it later.
Instead of trying to "click" the button, try submitting the form. The Javascript for that ConEdison page would be:
document.getElementById("form-login-email").form.submit()
In the "old days", with just a plain form, this would work fine.
But on that website there is a lot more going on. That email input element has nine events attached to it. By just filling in the value, you aren't triggering any of those events.
With forms built with ReactJS, for example, the user's interaction with the Input element is monitored, and the value is extracted and stored in a non-displaying object in the DOM tree. When the Submit button is clicked, the form's visible Input elements may not be used at all. In order to feed inputs into such forms, you need to understand ReactJS data structures, and manipulate them directly.
I recently built a scraper based on Chromedriver, which allows a Chrome browser window to be controlled from an external program, such as Access VBA. Once started, Chromedriver runs as a mini-webserver on localhost, and commands sent to it from Access VBA (through a ServerXMLHTTP60 object) cause it to launch Chrome, visit a URL, send keystrokes to input elements etc. Within the browser, the keystrokes fire all the events that human keystrokes would do. The target website was ReactJS-based, but I was able to ignore all of the internal complexity of ReactJS.
Since the website runs in a regular Chrome browser, I was able to use F12 Developer Tools in the development process.
The technology is Webdriver, and was developed for building website testing tools. There are Firefox and MS Edge Chromium versions as well.

Internet Explorer 11 automation VBA Document Empty

I am just starting to learn Internet Explorer Automation using VBA and I am facing an issue.
The Document object is empty which prevents me from going further than just loading a page. I have been looking around the net for a solution to this and could not find anything working so here I am, asking for your help.
NB: My VBA level is intermediate
Windows 8.1 pro 64 bit
I wrote an example code
I had a the same issue (nothing in the .document) , with Internet Explorer 11.
I had to add the site to the Compatibility View list (gear, compatibility view, add)
Hopefully this will help someone avoid the time I spent looking for a solution.
This code works for me. It uses a loop to wait until navigation is complete.
Sub NavigateGoogle()
Dim i As Long
Dim ie As InternetExplorer
Dim Googledoc As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "www.google.com"
Do
DoEvents
Loop While ie.Busy
Set Googledoc = ie.document
Debug.Print Googledoc.all(0).innerHTML
Set ie = Nothing
End Sub

Open a Webpage, Log In, and Close it again

I want to prepare daily reports from a particular JSON file. My plan is to download the file through VBA, then write a macro that automatically generates the report.
The problem is that the JSON file sit behind a Single-Sign-On authentication protocol. For various reasons I just do not want to VBA myself past the SSO.
What I would like to do is:
1) Have VBA open the address for the JSON file.
2) If I'm not already signed-in, open the browser window for the SSO page so I can manually put in my credentials.
3) Once the JSON file is open - if I'm already signed on and the data is there, to close the browser window and download the data to a particular location.
4) call the other macro so the report can be generated.
A: Is this possible?
B: How?
Yes it is possible and a little bit messy. As I don't know how your system indicate a successful login, I am assuming the login page is directed to an authenticated page.
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "Your login page" 'Go to login page
Do
DoEvents
Loop Until InStr(lcase(IE.LocationURL), lcase("Authenticated")) <> 0 ' wait until authentication is successful
IE.Navigate "your json url" ' Navigate to json url
Do
DoEvents
Loop Until IE.ReadyState = 4 ' wait until download is completed
Debug.Print IE.Document.body.innerHTML '<-- your json
IE.Quit
Set IE = Nothing

Automation error when getting ReadyState of InternetExplorer object

I get two different errors on the same line. Sometimes this one:
Automation error: object invoked has disconnected from its clients
and sometimes:
the interface is unknown
Minimal code to reproduce error:
Sub mcve()
Dim ie As Object
Dim www As String
Set ie = New InternetExplorerMedium
www = "http://www.stackoverflow.com"
ie.navigate www
ie.Visible = False
While ie.ReadyState <> 4 ' <~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs here
DoEvents
Wend
End Sub
This requires a reference: Tools > References... > Microsoft Internet Controls
The error occurs on While ie.ReadyState <> 4 the second time. How do I fix this?
This is a duplicate of a previously asked question. The problem seems to be caused by Internet Explorer security settings - when switching between security zones, the current instance of IE is killed and a new instance is created, so your reference to the old process is no longer valid.
Some of the suggested solutions were:
Change IE security settings. Uncheck "enable protected mode" on the Security tab of Internet Options.
Navigate to the IP address directly instead of the URL. This is the one that fixed it for me. For example, ie.navigate "64.233.177.106" (Google's IP address)
Set ie = New InternetExplorerMedium instead of New InternetExplorer. Or in your case, vice versa.
Instead of
Set ie = New InternetExplorerMedium
just use
Set ie = New InternetExplorer
or, for late binding:
Set ie = CreateObject("InternetExplorer.Application")
This makes the error go away.
I'm not sure why you would use InternetExplorerMedium in the first place. Quoting the small print in the documentation:
Remarks
Windows Internet Explorer 8. On Windows Vista, to create an instance of Internet Explorer running at a medium integrity level, pass CLSID_InternetExplorerMedium (defined in exdisp.idl) to CoCreateInstance. The resulting InternetExplorerMedium object supports the same events, methods, and properties as the InternetExplorer object.
Are you really using IE8 on Windows Vista, and do you really want "medium integrity level", whatever that means? I didn't think so.
Open Internet Explorer, then, go to internet settings, open "Sites" ande clear the web page that need the server comprobation. The problema is for the server comprobation.

Internet Explorer automation not working with IE11

I have some code that retrieves data from multiple websites via Internet Explorer automation in VBA. My code worked without problems with IE8, but in IE11, after the Navigate method of the Internet Explorer object is called, the Document and LocationURL are not updated; they still refer to the previously displayed website. Here's some code to reproduce the problem:
Sub Test()
Debug.Print "start"
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://en.wikipedia.org/wiki/Visual_Basic"
wait ie
Debug.Print "Current URL: " & ie.LocationURL
ie.Navigate "http://en.wikipedia.org/wiki/Microsoft_Office"
wait ie
Debug.Print "Current URL: " & ie.LocationURL
Set ie = Nothing
End Sub
Sub Wait(ie As Variant)
Do While ie.Busy
Application.wait DateAdd("s", 1, Now)
Loop
End Sub
When a run the above Test sub on a machine with IE8, it prints two different URLs, which is the expected behavior. However, when I run the same code on a machine with IE11, it prints the first URL twice. Any idea what might be wrong?
Update: I couldn't find a solution, so I went for the workaround of opening a new IE window for each URL.
I am not familiar with the VBA IE automation that you are using, however it sounds like you are hitting the same issue as Selenium WebDriver on IE11.
You may need to follow the same steps provided by the in the Selenium Wiki.
For IE 11 only, you will need to set a registry entry on the target
computer so that the driver can maintain a connection to the instance
of Internet Explorer it creates. For 32-bit Windows installations, the
key you must examine in the registry editor is
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows
installations, the key is
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the
FEATURE_BFCACHE subkey may or may not be present, and should be
created if it is not present. Important: Inside this key, create a
DWORD value named iexplore.exe with the value of 0.
http://code.google.com/p/selenium/wiki/InternetExplorerDriver
Hopefully that fixes your problem!
THX, You helped me.
W7 Ultimate 64bit czech, IE11, VBA in Microstation V8i
I use code like:
Public explorer As Object
....
Set explorer = CreateObject("InternetExplorer.Application")
....
If InStr(explorer.LocationURL, "CAPTCHA") = 0 Then
...
End If