VBA Binding modification - vba

I have tried to modify on this sample code given to me by ron
Sub test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.google.com"
With IE
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
' Find the desired url and click
Set Results = IE.document.getElementsByTagName("a")
For Each itm In Results
If itm.outerhtml = "B.A.C. VII" Then
itm.Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
Exit For
End If
Next
End Sub
Now i am not a fan of late binding so i am trying to modify the first two statements from
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
and replace them with
Dim IE As InternetExplorer.Application
Set IE = New InternetExplorer.Application
However this doesn't work on my code even if i have the Microsoft Internet Controls and the the Microsoft HTML Object Library activated on references as you can see in the picture below.
WHY THAT?
Dear mehow
I have thought of that but it gives me error just look

Dim ie As InternetExplorer
Set ie = new InternetExplorer
as InternetExplorer is the class name

Related

Sending a value of a mergefield from to a website

I have a mailmerg of patient specific information that is merged into an MSword document. I need to upload it to my hospital document repository. The repository puts the document in the correct place on our server by using the patient identification number. This patient identification number is one of the fields within the mail merge document. I am trying to send the patient id to the webpage without manually typing the ID into the web page. Can anyone help? I have tried to use VBA but I can't quite get the code correct
*Sub Macro1()
'
' Macro1 Macro
'
'Sub IE Automated
Dim ie As InternetExplorer
Dim sURL As String
sURL = "http://cedupload.southend.nhs.uk/"
Set ie = New InternetExplorer
With ie
.Top = 0
.Left = 0
.Width = 1000
.Height = 750
.AddressBar = False
.StatusBar = False
.ToolBar = 0
.Visible = True
.Navigate sURL
Do Until Not .Busy And .ReadyState = 4
DoEvents
Loop
.Document.all("PatientIdentifier") = ThisDocument.MailMerge.Fields.Item.Copy(Unit_No_)
Application.wait Now + TimeSerial(0, 0, 5)
End With
Do Until Not ie.Busy
DoEvents
Loop
ie.Document.all("btn btn-default").Click
Set ie = Nothing
Err.Clear
End Sub*

Scraping data from website using vba 4

I am trying to pull minimum nighttime temperature for London from the Met office website using a VBA web scrape. I've tried to use code as posted here. While the code runs it isnt copying what i need it be copying. Assistance would be much appreciated.
Sub WebScrape_1()
'Create an Internet Explorer browser
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
'Browse the browser to the target webpage
With appIE
.Navigate "https://www.metoffice.gov.uk/public/weather/forecast/gcpvj0v07"
.Visible = True ' False activate when happly with code
End With
'Wait while loading
Do While appIE.Busy
DoEvents
Loop
'What aspect of the webpage to copy
Set allRowOfData = appIE.document.getElementById("nightValue0")
Dim myValue As String: myValue = allRowOfData.innerHTML
'Close the browser
appIE.Quit
Set appIE = Nothing
'Paste the data into the selected range
ActiveWorkbook.Sheets("Data_Temp").Range("C7").Value = myValue
End Sub
All you have to do is add "FirstChild" and "innertext" to your code. If I were you I would write the code differently especially on the "wait while loading" piece. It is not an efficient way to do it. Anyways, find your working code below:
Sub WebScrape_1()
'Create an Internet Explorer browser
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
'Browse the browser to the target webpage
With appIE
.Navigate "https://www.metoffice.gov.uk/public/weather/forecast/gcpvj0v07"
.Visible = True ' False activate when happly with code
End With
'Wait while loading
Do While appIE.Busy
DoEvents
Loop
'What aspect of the webpage to copy
Set allrowofdata = appIE.document.getElementById("nightValue0").FirstChild
allrowofdata = allrowofdata.innertext
'Close the browser
appIE.Quit
Set appIE = Nothing
'Paste the data into the selected range
ActiveWorkbook.Sheets("Sheet1").Range("C7").Value = myValue
End Sub

Not able to login to a webpage using excel, error 424 object required

I am trying to login to this webpage, https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp
using VBA. Debugging shows me that the VBA throws an error 424 object required when username line is active (apparently it is not able to fill the username data).
Here's the code:
Sub Test()
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
With ie.document
.getElementById("txtUserId").Value = "ABCDE"
.getElementById("txtPassword").Value = "ABCDE"
.getElementById("submit").Click
End With
End Sub
Can anyone help me with debugging the problem while logging in to the given webpage?
Take a look at the below example:
Option Explicit
Sub Test()
Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.application")
With oIE
.Visible = True
.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
Do While .ReadyState < 4 Or .Busy
DoEvents
Loop
With .Document
Do While .ReadyState <> "complete"
DoEvents
Loop
With .parentWindow.frames("frmCUMain").document
.getElementsByName("txtUserId")(0).Value = "ABCDE"
.getElementsByName("txtPassword")(0).Value = "ABCDE"
.getElementsByName("cmdLogin")(0).Click
End With
End With
End With
End Sub

"Run-time automation error -2147417848 (80010108)"

I use VBA excel to parse a long list of local .htm files. The problem is that I get an error even before the programm starts to parse the HTM-files.
Error is:
VBA code:
<!-- language: lang-html -->
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub ImportHTM()
'Dim ie As InternetExplorer
Dim ie As InternetExplorerMedium
Dim html As HTMLDocument
Set ie = New InternetExplorerMedium
'Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "d:\Cloud\Dropbox\3.htm"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Loading Profile..." 'PROBLEM SEEMS TO BE HERE SOMEWHERE!
DoEvents
Loop
Set html = ie.document
Set ie = Nothing
Application.StatusBar = ""
'code code code --> which at this point isn't executed because the error occures before
Do you have any ideas what could cause the problem? Do you have any solution suggestions?
Also the command:
ie.Visible = False
doesn't seem to have any effect whatsoever since it opens the HTM-file in a new IE window.
Move the status bar update out of the loop.
Application.StatusBar = "Loading Profile..."
Do While ie.Busy Or ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Application.StatusBar = vbNullString
There is no need to rewrite the same message into the Application.StatusBar property hundreds if not thousands of times while you are waiting on a page load.
Regarding a new Internet.Explorer 'window' not inheriting the .Visible = False attribute, I recommend you switch to .Navigate2 and ShellWindows.
Addendum: Don't destroy your ie object until you are finished with the html associated with ie.document.

I can not get Web site text using VBA IE

I used the VBA below and monitored the variable IE.document. When the url is IE.navigate="http://www.mixi.jp", I could get all the web text using IE.document.all.
But in other sites like "http://www.yahoo.co.jp" , I could not get Web text. Why is that?
Sub Main()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://www.yahoo.co.jp"
Do While IE.Busy Or IE.readyState < 4
DoEvents
Loop
End Sub
Because this particular website's IE.document is nothing:
This seems to be the case with dynamically served sites, there are some suggestions here which I use below.
Although I am not sure you will be able to easily "get all of the text", you can certainly still iterate over the elements you're interested in extracting:
Sub Main()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.yahoo.co.jp"
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
'Create a collection of DIV elements for example
Dim myElements
DIm ele
Set myElements = ie.Document.Body.GetElementsByTagName("DIV")
'Then you can iterate over the DIV elements/etc., modify as needed.
For each ele in myElements
Debug.Print ele.InnerText
Next
End Sub