How to use Autoit in Excel VBA to automate window applications

How to use Autoit in Excel VBA to automate window applications

In this post, I’m going to tell you how to use Autoit in Excel VBA. Before dive into the topic let’s have a look at what is Autoit?

autoit in excel vba

Autoit is an open source automation tool forWindows GUI. It has its own BASIC scripting language for writing scripts, and is typically used in the administration, Quality Assurance, Maintenance, monitoring and system management fields for automating routine tasks. Autoit can manipulate the simulation of keyboard input, mouse movement and other controls to reliably automate tasks, unlike other programming languages

How to use AutoIt in excel VBA

To use autoit in excel VBA, you should have to first install it on your computer. You can download and install it before proceeding to next step.

Related Post:Selenium in Excel VBA

Open a new workbook and go the Developer tab or Alt+F11. Insert a new Module. Go to Tools->Reference and search for “AutoitX31.0 Type Library”.  Select the library and click Ok button.

Now you can invoke all the functions of AutoIt from Excel VBA. Let’s launch the calculator and play with it.

First, we need to create and initialize an object to invoke its functions.

Dim autoit As AutoItX3

Set autoit = New AutoItX3

Check whether AutoIt is installed on your machine.

If IsNull(autoit) Then

MsgBox “Autoit Is Not installed on your machine”, vbCritical +
vbOKOnly, “Verify”

Exit Sub

End If

Next, we need to launch the calculator using Run function by giving two parameters Name of the application and directory of the application. Add wait time for 2 seconds.

autoit.Run “calc.exe”,”C:WindowsSystem32″

autoit.Sleep “2000”

Check the existence of calculator and if exist then Click button “1”, “+”, “2” & “=” and then close the calculator application. To identify the properties of the button, you should use the window info tool.

To launch this tool go to Start -> All Programs ->AutoIt v3 ->Autoit Window Info tool.Under the control tab, you’ll find the Control ID of each button when you drag the finder tool and drop it on the respective button.

To understand how to find the properties of controls using window info tool check out this video.

If autoit.WinExists(“Calculator”,“”) Then

‘Click Button “1” and wait for oneSecond

autoit.ControlClick “Calculator”, “”,”131″

autoit.Sleep “1000”

‘Click Button “+” and wait for one Second

autoit.ControlClick “Calculator”, “”, “93”

autoit.Sleep “1000”

‘Click Button “2” and wait for one Second

autoit.ControlClick “Calculator”, “”,”132″

autoit.Sleep “1000”

‘Click Button “=” and wait for one Second

autoit.ControlClick “Calculator”, “”,”121″

autoit.Sleep “1000”

‘Close the calculator

autoit.WinClose “Calculator”, “”

End If

Source Code

Recommended Books:
AutoIT Scripting for Beginners

 

2 Comments

Leave a Reply