IE page doesn't update - VBA - vba

As far as I can see, there's nothing wrong with this code. Indeed, on one of my computers it works just fine, but on two others, it doesn't! All three run Excel 2003 on different versions of windows (7, 8.1 and 10).
What it is supposed to do is activate a search, then load the results page and print the innerHTML of the results page. One one machine (win 8.1) it does just that. On the other two, it prints the innerHTML of the search page.
Do I just have to abandon my trusty Excel 2003? Or is it a problem with my windows install? The W10 machine is a clean install of W10.
I am at my wits end over this! so please help me. :)
'This uses early binding
'In menu at top of page - Tools...Reference.... Microsoft Internet Controls and Microsoft HTML Object Library must both be ticked
Sub main()
sUrl = "https://www.insolvencydirect.bis.gov.uk/FIP1/"
Dim IE As InternetExplorer
Dim doc, element
Set IE = New InternetExplorer
IE.Visible = True
For iii = Asc("A") To Asc("B") ' *** test use B *** Do A-Z
aaa = Chr(iii)
IE.navigate sUrl
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set doc = IE.document
doc.getElementById("IPSurname").Value = aaa
doc.forms.Item(0).elements(7).Click
'loading new page with data
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set doc = IE.document
MyBit = doc.body.innerHTML
Debug.Print MyBit
Next
End Sub

choose some element tag from the page and do that :
.
.
IE.navigate sUrl
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set doc = IE.document
Do While True
If ie.document.getElementsByTagName("element tag").Length > 0 Then
Exit Do
End If
Application.Wait DateAdd("s", 1, Now)
Loop
.
.
.

Related

The remote server machine does not exist or is unavailable for specific website

I tried to open website using below VBA code:
Dim IE As Object
Dim doc As Object
Dim strURL As String
Set IE = CreateObject("InternetExplorer.Application")
With IE '
.Visible = True
.navigate "https://Google.com"
Do Until .readyState = 4
DoEvents
Loop
It's working for website like "google" etc.
But when I tried to open specific site like my company PLM " Agile (https://agileplm.XXXX.com/Agile/default/login-cms.jsp)" throwing error
"The remote server machine does not exist or is unavailable"
I could open the web page on explorer but throwing error while executing from below line
Do Until .readyState = 4
DoEvents
Loop
Is this due to any protection over site or not?
I used early binding on this and created the object InternetExplorerMedium rather than InternetExplorer and it seemed to work.
The issue is that Internet Explorer disconnects from VBA (In my case some internal security settings). The solution is to reconnect Internet Explorer to VBA:
Include this code after the line IE is not responsive anymore and the existing Internet Explorer window will be assigned to = IEObject1
Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
Set shellWins = New SHDocVw.ShellWindows
For Each explorer In shellWins
If explorer.Name = "Internet Explorer" Then
Set IEObject1 = explorer
Debug.Print explorer.LocationURL
Debug.Print explorer.LocationName
End If
Next
Set shellWins = Nothing
Set explorer = Nothing
If you have several IE windows open then this code picks the last one. You can choose between the windows by using the URL or LocationName if you need several open windows.
Try to make a test with example code below may help you to solve your issue.
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://agileplm.xxxx.com/Agile/default/login-cms.jsp"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
Reference:
Automate Internet Explorer (IE) Using VBA
If your issue persist than try to provide a detailed information, Whether you are having issue with opening the page or you got an error while checking the ready state of IE. We will try to provide further suggestions.
Dim IE As Object
Dim doc As Object
Dim strURL As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://Google.com"
End With
For Each win In CreateObject("Shell.Application").Windows
If win.Name Like "*Internet Explorer" Then
Set IE = win: Exit For
End If
Next
With IE
Do Until .readyState = 4
DoEvents
Loop

Find form ID on a website and input value using VBA

I would require a bit of help finding a form id on a public website (http://www.medicines.ie/). They updated the site and the previous id no longer works since now there is no Id to be found. What I am trying to do is open this site with VBA, input the value from a specific cell in excel to a form (textbox) on this website and press the search button. I am using the code below:
Sub Medicinesie()
Dim IE As Object
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
IE.navigate "http://www.medicines.ie/"
IE.Visible = True
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
IE.Document.getElementById("input").Value = Range("spc") '<---- spc is the name of the cell I am referencing
IE.Document.forms(0).submit
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
End Sub
It looks like you could take advantage of the URL construction at this website. URL is constructed:
http://www.medicines.ie/medicines?page=1&per-page=25&query= + anything you would like to search in this database.
Sub MedicineS()
Dim IE As Object
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
Dim URL As String
URL = "http://www.medicines.ie/medicines?page=1&per-page=25&query=" & _
Range("spc")
IE.Visible = True
IE.navigate URL
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
End Sub
However if you still prefer to use your own way keep in mind that input you are looking for is 4th in the code so:
IE.Document.getElementByTagName("input")(3).Value
and the button is second so:
IE.Document.getElementByTagName("button")(1).Click
With selenium vba wrapper installed and adding tools > reference > selenium type library
Option Explicit
Public Sub test()
Dim d As WebDriver
Set d = New ChromeDriver '<== can change to internet explorer driver
With d
.Start "Chrome"
.Get "http://www.medicines.ie/"
.FindElementByCss("input.search__input").SendKeys "Aspirin" '<== Range("spc")
.FindElementByTag("form").Submit
Stop
'.Quit
End With
End Sub
Example run:

Clicking link within a HTML List

I'm trying use Excel's IE automation tools to click a website link within a users profile of this site. Using the following in VBA:
ie.Document.getElementsByClassName("website")(0).getElementsByTagName("a")(0).Click
But keep getting the Runtime error '438' when ran. Any advice on how to fix this or if clicking this link is even possible please? Thanks.
EDIT (Full Code):
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "site"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set objA = ie.Document.getElementsByClassName("website")(0) 'GETTIN ERROR HERE
objA.getElementsByTagName("a")(0).Click
End Sub
It would appear that there were a few minor issues here.
One being that .Click doesn't always function properly, I would suggest using .getAttribute("href") combined with ie.Navigate()
Another being that you seem to define html then never use it again.
The following code works:
Sub test()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://beverlyhills.yourkwoffice.com/mcj/user/AssociateSearchSubmitAction.do?orgId=5058&lastName=&firstName=&rows=100"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.Document
Set objA = html.getElementsByClassName("website")(0).getElementsByTagName("a")(0)
ie.Navigate (objA.getAttribute("href"))
End Sub

Click button of java build website using VBA code

I;m in the process of learning the web scraping using VBA and I've come across the website listed within my code which is Java build.
My goal is to click Next button from the VBA code, but I can not identify it when looking at "behind the scene" code. When using "Inspect" element i see the button reference but when I list all the links in my excel it is not there.
Then I looked at the page using CTR-U (in Chrome) and the page looks completely different and has a lot of Java Code. I am not familiar with Java therefore could you please describe how is the mechanism of decoding such a Java page?
Here is my code:
Sub first_template()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Top = 0
ie.Left = 0
ie.Visible = True
ie.navigate "http://www.amaaonline.com/member-directory/"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set input_elements = ie.document.getElementsByTagName("a")
i = 4
For Each element In input_elements
Cells(i, 1).Value = element.innerHTML
i = i + 1
Next element
ie.Quit
Set ie = Nothing
End Sub

VBA WebScraping content from nested frames: access denied

This isn't an important task, but it is one that I thought would be easy and has instead been very frustrating. I'm trying to grab the current print counter values from our MFP but I am not able to get past the second Frame on the page.
In fact, it seems to continually loop me back to the top as I try to go "deeper" into the page.
The data I am after is stored in the "contents" frame.
HTML Source - Nested Frames
Sub Copy_Count()
Dim IE As InternetExplorerMedium
Dim strURL() As String
Dim HTML_Doc As HTMLDocument
Dim HTML_Doc2 As HTMLDocument
' There will be additional machines to retrieve data from, current copier must navigate to main page first before counter.
strURL = Split("http://192.168.50.26/?MAIN=DEVICE,http://192.168.50.26/?MAIN=COUNTER&SUB=TOTAL", ",")
Set IE = New InternetExplorerMedium
IE.Navigate2 strURL(0)
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
IE.Navigate2 strURL(1)
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Set HTML_Doc = IE.Document
' This get's me to "TopLevelFrame"
Debug.Print HTML_Doc.getElementsByTagName("frameset")(0).getElementsByTagName("frame")(0).Document.Body.innerHTML
' Assign the document of the TopLevelFrame
Set HTML_Doc2 = HTML_Doc.getElementsByTagName("frameset")(0).getElementsByTagName("frame")(0).Document
Debug.Print HTML_Doc2.getElementById("TotalFullColor")
Debug.Print HTML_Doc.getElementById("TotalFullColor").innerText
Debug.Print IE.Document.GetElementsByID("TotalFullColor")(1)
End Sub
Windows 10 Pro, IE 11, Office Pro Plus 2016