I am trying running a piece of VBA code in excel 2013 to access some data from our intranet site and I am getting a run time error 2147467259 (80004005) - Automation error unspecified error.
Sub AccessWar()
Dim oIE As InternetExplorer
Dim oHTML As HTMLDocument
Set oIE = New InternetExplorer
oIE.Navigate ("http://iscls3apps/WAR40/aspx/WAR40AllReports.aspx")
oHTML = oIE.document
End Sub
I am new to excel VBA and do not have much idea. Any pointers would be helpful
I tried to use IE with VBA too but it seemed that the running don't really care if the loading of the page in IE is done or not!
So I made i little routine that navigate and wait, just by adding
Application.Wait(Now + TimeValue("0:00:05"))
after every navigate... No super efficient but VBA is not made to parse information on the web, you could use so more approriate languages if you are willing to! ;)
Related
I have a vba program that uses InternetExplorer and HTMLDocument object. Since Microsoft has already announced that it will shutdown internet explorer starting June 15, 2022. I am just worried will the vba program that I did still works? Since I'm not sure if they will be shutting down IE completely.
Option Explicit
Sub automateIE()
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim URL As String
Set IE = New InternetExplorer
Let URL = "https://www.simpleexcelvba.com/"
IE.Visible = True
IE.navigate URL
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc = IE.Document
doc.getElementsByName("s").Item(0).Value = "Connect to SAP"
doc.getElementsByClassName("search-submit").Item(0).Click
End Sub
Yeah, this was a big concern for our team when this was announced a while back. We had over 50 VBA bots that used IE and migrated them over to Power Automate Desktop. Microsoft's new RPA software that works with most of the main browsers. The other option is to download selenium for vba (a little outdated but still works) or head over to python to use selenium which is a stronger solution anyway.
I'm a beginner with both VBA and Selenium so forgive me if this is too obvious. I'm trying to see whether I can make VBA open a Chrome window to check that Selenium works fine, via the code below:
Option Explicit
Private MyBrowser As Selenium.ChromeDriver
Sub TestSelenium()
Set MyBrowser = New Selenium.ChromeDriver
MyBrowser.Start
End Sub
I, however, get the following error:
Does anyone know what I could be doing wrong?
Thanks so much
As it turns out I had to download Microsoft .NET 3.5 Framework for it to work
To prepare for the eventual 'going away' of IE11, I've been trying to figure out how to replace a couple parts of my code. One involves launching IE and using that browser to scrape some pages. Is there an equivalent way to do the below in Edge? I don't see a way to add a reference to the Edge libraries like I did with 'Microsoft Internet Objects' and IE11.
Dim ie As InternetExplorerMedium: Set ie = New InternetExplorerMedium
Dim html As HTMLDocument
With ie
.Visible = False
.Navigate website 'string that's created above this code
End With
Do While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait Now + #12:00:10 AM#
Set html = ie.Document
Thanks everyone for your help.
Ok, a few explanations. I am writing these as a reply so as not to have to split them into several comments.
Does Edge work instead of IE to do web scraping with VBA?
It does not work directly. The reason is that IE has a COM interface (Wikipedia: Component Object Model). No other browser has this interface. Not even Edge.
But for Edge there is also a web driver for Selenium. Even provided directly by MS.
Another alternative - xhr
Since you can't use Selenium because you don't have admin rights, there might be the possibility to use xhr (XML HTTP Request). However, in order to make a statement on this, we would have to know the page that you want to scrape.
Xhr can be used directly from VBA because it does not use a browser. The big limitation is that only static content can be processed. No JavaScript is executed, so nothing is reloaded or generated dynamically in any other way. On the other hand, this option is much faster than browser solutions. Often, a static file provided by the web server is sufficient. This can be an HTML file, a JSON or another data exchange format.
There are many examples of using xhr with VBA here on SO. Take note of the possibility first as another approach. I can't explain the method exhaustively here, also because I don't know everything about it myself. But there are many ways to use it.
By the way
IE will finally be discontinued in June 2022 and will then also no longer be delivered with Windows. That's what I read on the German IT pages a few days ago. But there are already massive restrictions on the use of IE.
I'm trying to download a complete webpage. In other words automate this process:
1- Open the webpage
2- Click on Save as
3- Select Complete
4- Close the webpage.
This is what I've got so far:
URL = "google.com" 'for TEST
Dim IE
Set IE = CreateObject("Internetexplorer.Application")
IE.Visible = False
IE.Navigate URL
Do
Loop While IE.Busy = True
Dim i
Dim Filename
i = 0
Filename = "C:\Test.htm"
IE.Document.ExecCommand "SaveAs", False, Filename
When I run the code in the last line a save file dialog appears. Is there any way to suppress this?
Any help would be most appreciated.
The Save As dialog cannot be suppressed:
The Save HTML Document dialog cannot be suppressed when calling this method from script.
It is also a modal dialog and you cannot automate the way to click the "Save" button. VBA execution pauses while waiting manual user input when faced with a dialog of this sort.
Rather than using the IE.Document.ExecCommand method, you could try to read the page's HTML and print that to a file using standard I/O functions.
Option Explicit
Sub SaveHTML()
Dim URL as String
Dim IE as Object
Dim i as Long
Dim FileName as String
Dim FF as Integer
URL = "http://google.com" 'for TEST
Filename = "C:\Test.htm"
Set IE = CreateObject("Internetexplorer.Application")
IE.Visible = True
IE.Navigate URL
Do
Loop While IE.Busy
'Creates a file as specified
' this will overwrite an existing file if already exists
CreateObject("Scripting.FileSystemObject").CreateTextFile FileName
FF = FreeFile
Open Filename For Output As #FF
With IE.Document.Body
Print #FF, .OuterHtml & .InnerHtml
End With
Close #FF
IE.Quit
Set IE = Nothing
End Sub
I am not sure whether this will give you exactly what you want, or not. There are other ways to get data from web and probably the best would be to get the raw HTML from an XMLHTTP request and print that to a file.
Of course, it is rarely the case that we actually need an entire web page in HTML format, so if you are looking to then scrape particular data from a web page, the XMLHTTP and DOM would be the best way to do this, and it's not necessary to save this to a file at all.
Or, you could use the Selenium wrapper to automate IE, which is much more robust than using the relatively few native methods to the InternetExplorer.Application class.
Note also that you are using a rather crude method of waiting for the web page to load (Loop While IE.Busy). While this may work sometimes, it may not be reliable. There are dozens of questions about how to do this properly here on SO, so I would refer you to the search feature here to tweak that code a little bit.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Using an IE browser with Visual Basic
I have a website that is updated daily. I need to retrieve the information from this website daily. Instead of opening up a new browser eg new internet explorer everyday, is it possible to use an already opened internet explorer to retrieve the information.
I don't have IE installed so I can't promise this will work, but give it a shot. Note that you'll need to set references to Microsoft Internet Controls and Microsoft HTML Object Library.
Function GetOpenIE() As SHDocVw.InternetExplorer
Dim ie As SHDocVw.InternetExplorer
Dim sw As SHDocVw.shellWindows
Set sw = New SHDocVw.shellWindows
For Each ie In sw
If TypeOf ie.Document Is HTMLDocument Then
Set GetOpenIE = ie
Exit Function
End If
Next ie
End Function