Visual Basic 2008/2010 - How do I automatically click buttons in a sequence?

I have about 15 buttons that automatically goto a website, then it clicks on certain links and does save as page. They are working beautifully, now i have to automate this process.

I already have a code in place to wait for the website to finish with each button so i am not sure if i can still use a timer to complete this process?

please help

Recommended Answers

All 4 Replies

There is probably a solution that can be used from inside VB, but if one is not forthcoming I can suggest AutoIt. I have used this free (and very well supported) package to automate many tasks. You can write scripts using AutoIt's native scripting language, or you can write them in vbScript (which is what I do). If you have a control (label, etc) that provides feedback as to when an action completes (after, say, clicking on a button) then AutoIt can read the value of that control. As an example, let's say you have a label which has the value of "WORKING" or "COMPLETED". AutoIt could "click" the button, then wait until the value of the label changes to "COMPLETED" before going on to click the next button. If you add another state ("ERROR") then AutoIt could also detect that and stop/pause for user action.

The AutoIt process itself could be triggered from inside the VB app to start the ball rolling. AutoIt is available (free) here.

Afterthought - because AutoIt can be run as an ActiveX control, it can be created and manipulated from within VB so there should be a way to do everything internally with no external scripting required. If you need help with this I'd be happy to spend some time investigating.

I got bored. Lucky for you ;)

I threw together a small project to show how you can automate a sequence of button clicks using AutoIt. Start with a blank form. Add three buttons named Button1, Button2, Button3. Add another button named btnStart and a multi-line text box named txtResults. Now replace the form code with the following:

'                                                                                                   
'  Name:                                                                                            
'                                                                                                   
'    AutoClick                                                                                      
'                                                                                                   
'  Description:                                                                                     
'                                                                                                   
'    Demonstrates how to automate the clicking of buttons. Buttons 1, 2 and 3 are clicked in        
'    sequence. A custom event is used to avoid wait loops and polling. The completion of each       
'    button click raises the custom event to proceed to the next step in the automation.            
'                                                                                                   
'  Externals:                                                                                       
'                                                                                                   
'    Requires AutoIt (www.autoit.com)                                                               
'    Add a reference to AutoItX3.dll                                                                
'                                                                                                   
'  Audit:                                                                                           
'                                                                                                   
'    2012-02-23  R. Jim original code                                                               
'                                                                                                   

Public Class Form1

    Private autoit As New AutoItX3Lib.AutoItX3      'automation control                             
    Private Event ClickDone()                       'custom event for step completion               
    Private sequence() As String                    'sequence of buttons to click                   
    Private seqindex As Integer                     'index into button sequence                     

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        DirectCast(sender, Button).BackColor = Color.Aqua
        txtResults.AppendText(DirectCast(sender, Button).Name & " clicked" & vbCrLf)
        RaiseEvent ClickDone()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        DirectCast(sender, Button).BackColor = Color.Aqua
        txtResults.AppendText(DirectCast(sender, Button).Name & " clicked" & vbCrLf)
        RaiseEvent ClickDone()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        DirectCast(sender, Button).BackColor = Color.Aqua
        txtResults.AppendText(DirectCast(sender, Button).Name & " clicked" & vbCrLf)
        RaiseEvent ClickDone()
    End Sub

    Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click

        'First we define the order in which we want to click the buttons. We start with sequence    
        'index -1 because the first thing the ClickDone handler does is increment this sequence     
        'number (to determine the next button to click). If we start with -1 then after the first   
        'increment, the next button will be the first (zeroth) one.                                 

        sequence = {"Button1", "Button2", "Button3"}        'define the button sequence             
        seqindex = -1                                       'set to start with first button         
        RaiseEvent ClickDone()                              'trigger next button                    

    End Sub

    Private Sub ClickIt() Handles Me.ClickDone

        'This handler assumes that there is a series of button names defined as "sequence" and an   
        'index into this series defined as "seqindex". Each time this event is raised it increments 
        'the sequence index and (if there are more buttons in the sequence), clicks the next button.

        seqindex += 1
        If seqindex <= UBound(sequence) Then
            autoit.ControlClick(Me.Text, "", sequence(seqindex))
        End If

    End Sub

End Class

When you click on "Start", the three buttons will turn blue and txtResults will get three lines of text indicating that the three buttons have been clicked. I couldn't think of anything useful and time consuming to put in the Button1, etc click handlers. Add whatever code you like prior to the line with RaiseEvent ClickDone()

Just ensure that RaiseEvent ClickDone() is the last line. Obviously, you'll want to be able to use the form with or without automation so you will need a class boolean to indicate whether you are running in auto or manual in which case you would end off each button with

If auto Then
    RaiseEvent ClickDone()
End If

If this fixed your problem then please mark this thread as solved.

In case you are still subscribed to this thread, I came across a button method that can also be used to click a button via code. That method is PerformClick and is used by

btnMyButton.PerformClick()

This will trigger the same event as if you had clicked the button manually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.