Excel VBA click button on a web site

Steveie

New Member
Joined
Nov 25, 2011
Messages
29
Hello,

Would be extremely grateful if anybody could help me with this.

I'm trying to automate some work tasks using Excel VBA to run reports off a website. I'm teaching myself as I go and have automated the access, navigation, clicking buttons with ID's but am stumped on this and getting nowhere fast 'Googling' it.

I have a button I need to click which doesn't have an ID like other ones I have encountered. The element for it appears as below:

HTML:
<input name="csvset" title="Export" class="btn" type="submit" value="Export">

I'm trying to understand HTML code as I go but am unsure on this. From what I have read I believe it may be you can reference it by name or it may be a form that needs to be submitted. On the web site it appears as a click button.

Can anybody point me in the right direction of how I reference it in VBA to 'click' it please ?

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If it's a button to submit a form then you can submit the form rather than click the button.

Don't know what your current code looks like but let's say IE is an instance of Internet Explorer and you've navigated to the page with the button you want to click/form you want to submit.

This code would submit the first form on the page.
Code:
IE.document.forms(0).submit
 
Last edited:
Upvote 0
Hi Thanks Norie,

Unfortunately that didn't work, it did take me to another page but not the correct one, it said search criteria were missing and took me to a search page, not one I would be expecting.

The button to click on is an 'Export Data' button that then goes to another page and the option to select the file type prior to clicking another export button.

Where the export data button is that I need to click in this instance there are also 7 other buttons so I guess I need to specifically reference this one maybe by name maybe ?

I have shared the code I have below but have had to edit parts to hide so work references.


Code:
Sub RunData()

Dim IE As InternetExplorerMedium 
Dim targetURL As String
Dim webContent As String
Dim sh
Dim eachIE
Dim targetURLCallsMade As String


targetURL = "https://"
Set IE = New InternetExplorerMedium
IE.Visible = True 
IE.navigate targetURL


While IE.Busy
  DoEvents
  Wend


Do
  Set sh = New Shell32.Shell
  For Each eachIE In sh.Windows
    If InStr(1, eachIE.LocationURL, targetURL) Then
      Set IE = eachIE
      'IE.Visible = False 
      Exit Do
      End If
    Next eachIE
  Loop
Set eachIE = Nothing
Set sh = Nothing


While IE.Busy  ' The new process may still be busy even after you find it
  DoEvents
  Wend


' Input the userid and password
    IE.Document.getElementById("username").Value = "**@**.com"
    IE.Document.getElementById("password").Value = "**"


' Click the "Login" button
    IE.Document.getElementById("Login").Click


    Do Until Not IE.Busy And IE.ReadyState = 4
        DoEvents
    Loop
    
    Application.Wait (Now + TimeValue("00:00:25"))


' Click the "Reports" section
    'IE.Document.getElementById("report_Tab").Click


    'Do Until Not IE.Busy And IE.ReadyState = 4
        'DoEvents
    'Loop


targetURLCallsMade = "https://**"
IE.navigate targetURLData


Do Until Not IE.Busy And IE.ReadyState = 4
        DoEvents
    Loop
    
' Select From Data
    IE.Document.getElementById("colDt_s").Value = "01/02/2016"
    IE.Document.getElementById("colDt_e").Value = "13/03/2016"




Do Until Not IE.Busy And IE.ReadyState = 4
        DoEvents
    Loop




IE.Document.Forms(0).submit


Do Until Not IE.Busy And IE.ReadyState = 4
        DoEvents
    Loop




End Sub
 
Upvote 0
Check out this HTML reference to understand what the button is doing:

HTML input tag

What we can learn here is that it must be a member of a Form.
It's action is to submit the form and it does this with a value of "Export".
NOTE: The other buttons might submit the form with other values.

Be sure to pass the value "Export" to the correct form.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top