I have managed with opening the Internet Explorer using VBA Excel.
My code looks as follows:
Sub IE()
Dim ie As object
Dim location
'Dim button
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate ("https://fulcrum/NewBuild/Record.aspx?ID=" & Range("B1").Value)
.Navigate ("http://gcommswebmapgb/portal/")
.Top = 5
.Left = 5
.Height = 1300
.Width = 1900
While ie.ReadyState <>4
Do Events
Wend
Set location = .document.getElementById("__VIEWSTATE")
'Set button = .document.getElementById("btnContainer").Children(0)
'button.Click
While ie.ReadyState <> 4
DoEvents
Wend
End With
Set ie = Nothing
End Sub
In this event, the 2nd link is opened and 1st one completely omitted.
On top of that, I would like to open them in the same window (as a different tab).
I found some solutions here:
Excel VBA control IE
VBA Excel input data into already opened ie window
where it looks that some functions are required.
Is anyone able to help?
I suggest you store both URLs in a variables like below.
Dim url1, url2 As String
url1 = "https://fulcrum/NewBuild/Record.aspx?ID=" & Range("B1").Value
url2 = "http://gcommswebmapgb/portal/"
Then try to pass the URL variables to .Navigate without round brackets like below.
With ie
.Visible = True
.Navigate url1
.Navigate url2, CLng(2048)
It will fix your syntax error and urls will be opened in 2 tabs.
Modified code:
Sub ie()
Dim ie As Object
Dim location
'Dim button
Dim url1, url2 As String
url1 = "https://fulcrum/NewBuild/Record.aspx?ID=" & Range("B1").Value
url2 = "http://gcommswebmapgb/portal/"
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate url1
.Navigate url2, CLng(2048)
' .Top = 5
' .Left = 5
' .Height = 1300
' .Width = 1900
While ie.ReadyState <> 4
'Do Events
Wend
'Set location = .document.getElementById("__VIEWSTATE")
'Set button = .document.getElementById("btnContainer").Children(0)
'button.Click
While ie.ReadyState <> 4
DoEvents
Wend
End With
Set ie = Nothing
End Sub
Output:
Further, you need to check that your code properly references the correct tab to execute further code. Note that you cannot automate both pages at the same time. You need to switch the tabs using your VBA code to execute code on specific page.
Related
enter image description herei am trying to download an excel file(.xls) from a pop up in Internet explorer via vba code. Which has three options (Open / Save / Cancel). i know this question was asked by many i tried few and it didnt work. Could you guys help ? i am using IE 11 version. Below is my code :
CODE :
Sub Button1_Click()
Dim IE As Object
Set IE = New InternetExplorerMedium
With IE
.Visible = True
.Navigate
"http://home.tim.flextronics.com/timireptool/ItemTransfer/Default.aspx"
Do While .busy
DoEvents
Loop
Do While .readystate <> 4
DoEvents
Loop
End With
Set CPC = IE.document.getelementbyid("tbCPC")
CPC.Value = "ALC3"
Set ddlOwner = IE.document.getelementbyid("ddlOwner")
For i = 1 To ddlOwner.Options.Length
If ddlOwner.Options(i).Text = "Warehouse" Then
ddlOwner.selectedindex = i
Exit For
End If
Next i
Set butview = IE.document.getelementbyid("butView")
butview.Click
Do While IE.busy
DoEvents
Loop
Do While IE.readystate <> 4
DoEvents
Loop
Set butExcel = IE.document.getelementbyid("butExcel")
butExcel.Click
Do While IE.busy
DoEvents
Loop
<<WHERE I WANT TO DOWNLOAD FROM THE POP>>
End Sub
Suggest me a solution to OPEN/SAVE the file. Thanks.
I want to use excel vba to control Internet explorer object.
Manually we can setup in "https://www.investing.com/indices/us-30-historical-data"
Time Frame i.e. "Daily"
Start Date i.e. "01/01/2016"
End Date i.e "31/12/2016"
How to do this with excel vba ?
I've this code to open the URL
InfiniteLoop = True
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate myURLLink ' should work for any URL
SleepTime = 10000
Do
DoEvents
Sleep (SleepTime)
If ie.ReadyState >= 4 Then
Exit Do
End If
Loop Until InfiniteLoop = False
End With
But I don't know how to pass
Time Frame i.e. "Daily"
Start Date i.e. "01/01/2016"
Start Date i.e. "01/01/2016"
Can anyone help ?
I have came up with this simple macro (Excel 2010) to navigate to the sit and change the dates and data interval, hopefully it will help:
Sub test()
Dim IE As InternetExplorer
Dim IEdoc As Object
Dim IEElement As Object
Set IE = New InternetExplorer 'initialize Internet Explorer
IE.Navigate "https://www.investing.com/indices/us-30-historical-data" 'Navigate to URL
IE.Visible = True
Set IEdoc = IE.Document
Set IEElement = IEdoc.GEtelementbyID("picker") ' navigate to date input
IEElement.Value = "01/01/2016 - 01/01/2017"
Set IEElement = IEdoc.GEtelementbyID("widget") ' navigate to date widget
IEElement.Click
Set IEElement = IEdoc.GEtelementbyID("applyBtn") ' navigate to Apply button to save the dates
IEElement.Click
Set IEElement = IEdoc.GEtelementbyID("data_interval") ' navigate to Interval picker
IEElement.Value = "Daily"
End Sub
I have a short excel macro that is designed to:
1) Open Internet Explorer and navigate to "http://www.puco.ohio.gov/pucogis/address/search.cfm"
2) Fill out a form on that site with data from the excel workbook
3) Click a button to submit the form
4) Scrape some innertext from the website and place it in a cell in the workbook
5) Close Internet Explorer
I can not get step 3 to work. That is, I can not get the click/submit function to work with this website. When the button is clicked the website populates with information specific to the information entered in the form. Everything else in the code is working. I have searched for an answer and tried the submit verses click approach with no luck.
Thanks for you help.
Code below:
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Set HTMLButton = ie.Document.getElementsByTagName("input")(1)
HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
Try this solution, which I found from this answer to a similar question. That answer was not accepted, but I have tested this with your code and seems to be working.
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Call ie.Document.parentWindow.execScript("codeAddress()")
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
If you don't know or can't reasonably anticipate the function call codeAddress(), then you can try something like this to derive it from the button's onclick property:
Dim fn$
fn = HTMLButton.onclick
fn = Mid(fn, InStr(fn, "{"))
fn = Trim(Replace(Replace(Replace(fn, "{", vbNullString), "}", vbNullString), vbLf, vbNullString))
Call ie.Document.parentWindow.execScript(fn)
You can call the JavaScript directly. try this it will work
Instead of:
Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
HTMLButton.Click
use
ie.Document.parentWindow.execScript code:="codeAddress()"
note that IE may prompt you to confirm every run so you may need to
stop showing this message for smooth operation
Private Sub CommandButton1_Click()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
ie.Document.parentWindow.execScript code:="codeAddress()"
'Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
'HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D16") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
thanks also to this article helped me to solve your problem
How to find and call javascript method from vba
I'm trying to get the currency exchange rate offered by WorldRemit for a pair of currencies. I want to change the value in the 'Send From' dropdown list on the top left corner of the webpage. (https://www.worldremit.com/en/South-Africa)
I wasn't able to select the dropdown option using .Selected = True or .Click so have used .SelectedIndex. After selecting the value, I'm not able to trigger the change event that refreshes the page. Would be great if someone can help me figure this out.
Code for navigating to the page:
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://www.worldremit.com/en/South-Africa"
While ie.busy
DoEvents
Wend
Set HTMLdoc = ie.document
Code for selecting option using .SelectedIndex property:
Dim fromSelect As HTMLSelectElement
Set fromSelect = HTMLdoc.getElementById("selectFrom")
optionIndex = Find_Select_Option(fromSelect, "Germany")
If optionIndex >= 0 Then
fromSelect.selectedIndex = optionIndex
fromSelect.FireEvent ("onchange") ' this doesn't work
End If
Function for selecting option (used in the code above):
Function Find_Select_Option(selectElement As HTMLSelectElement, optionText As String) As Integer
Dim i As Integer
Find_Select_Option = -1
i = 0
While i < selectElement.Options.Length And Find_Select_Option = -1
DoEvents
If LCase(Trim(selectElement.Item(i).Text)) = LCase(Trim(optionText)) Then Find_Select_Option = i
i = i + 1
Wend
End Function
Edit #1 : The HTML snippet containing the element in question is
<select id="selectFrom" data-track-field-name="from country" data-track-event="change">...</select>
Give this a try, it's working on my end. Sometimes using jQuery is a bit easier, especially when the page also uses jQuery as it does here.
You can use jQuery by using the execScript function of IE. See below:
Public Sub test()
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://www.worldremit.com/en/South-Africa"
'Wait for the page to load
While .busy Or .readyState <> 4
Application.Wait (Now() + TimeValue("00:00:01"))
DoEvents
Wend
'Use JQuery to find the element based on ID, then make the Selected property true
'Once that is done, call the change event in jQuery
.document.parentWindow.execScript "$('#selectFrom option:contains(Germany)').prop('selected','True')"
.document.parentWindow.execScript "$('#selectFrom option:contains(Germany)').change()"
End With
End Sub
Apparently FireEvent doesn't work all that well with IE 11 so need to use CreatEvent + initEvent + dispatchEvent
Working code snippet below:
Dim fromSelect As HTMLSelectElement
Dim evt As Object
Set evt = HTMLdoc.createEvent("HTMLEvents")
evt.initEvent "change", True, False
Set fromSelect = HTMLdoc.getElementById("selectFrom")
optionIndex = Find_Select_Option(fromSelect, "Germany")
If optionIndex >= 0 Then
fromSelect.selectedIndex = optionIndex
fromSelect.dispatchEvent evt
End If
I am a newbie in this website and in VBA programming as well. I am stuck into a problem where I have to fetch the data from this page. I need to have the hyperlink url of Check Rates 10 button. Can anyone help me with this problem.
I have done the following code:
Sub GetData()
Dim IE As New InternetExplorer
IE.navigate "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345"
IE.Visible = False
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Dim doc As HTMLDocument 'variable for document or data which need to be extracted out of webpage
Set doc = IE.document
Dim dd As Variant
dd = doc.getElementsByClassName("lgn")(0).outerHtml
'Range("a1").Value = dd
MsgBox dd
End Sub
In which I am getting text of the button but I want to have the value of the class. I think I am very close to the result but somehow cant reach to the goal...can anyone please help me...
Regards,
I think this is what you're looking for:
(Code modified slightly from Kyle's answer here)
Sub Test()
'Must have the Microsoft HTML Object Library reference enabled
Dim oHtml As HTMLDocument
Dim oElement As Object
Dim link As String
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345", False
.Send
oHtml.Body.innerHTML = .responseText
End With
If InStr(1, oHtml.getElementsByClassName("lgn")(0).innerText, "Bekijk 10 prijzen") > 0 Then
link = Mid(oHtml.getElementsByClassName("lgn")(0).href, 7)
Debug.Print "http://www.kieskeurig.nl" & link
End If
End Sub
This code prints the URL to the immediate window. Hope that helps!
This works for me...
Sub GetData()
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345"
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
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set Results = IE.document.getElementsByTagName("a")
For Each itm In Results
If itm.classname = "lgn" Then
dd = itm.getAttribute("href")
Exit For
End If
Next
' if you wnat to click the link
itm.Click
' otherwise
'Range("a1").Value = dd
MsgBox dd
End Sub