Web Automation Using Vba

Embed Size (px)

Citation preview

  • 8/2/2019 Web Automation Using Vba

    1/13

    By Rohit Hambar

  • 8/2/2019 Web Automation Using Vba

    2/13

    What is VBA ? Macros

    Example

  • 8/2/2019 Web Automation Using Vba

    3/13

    Visual Basic for Applications (VBA) is animplementation of Microsoft's event drivenprogramming language Visual Basic 6 and its

    associated integrated developmentenvironment (IDE), which are built into mostMicrosoft Office applications.

    VBA enables building user defined functions,

    automating processes and accessing WindowsAPI and other low-level functionality throughdynamic-link libraries (DLLs).

  • 8/2/2019 Web Automation Using Vba

    4/13

    It can be used to control many aspects of thehost application, including manipulating userinterface features, such as menus and toolbars,

    and working with custom user forms or dialogboxes.

    VBA can also be used to create import andexport filters for various file formats, such as

    OpenDocument (ODF).

  • 8/2/2019 Web Automation Using Vba

    5/13

    If you need to do repeated tasks in excel every day,then it is always recommend to automate suchtasks so that you can save time and energy.

    Macros are a great way of automating tasks inExcel, we have already covered a basic tip onrecording and using Macros. Macros basically addVB code to your excel sheet.

    Whenever you create a new Macro, Excel addsvisual basic code at the back-end, so whenever yourun a Macro, this VB code is executed and you getthe results on the screen.

  • 8/2/2019 Web Automation Using Vba

    6/13

    To open Google Search page in IE using VBAfor input string.

    First thing to know is how to open IE .The best

    way is probably to create the object and make itvisible. Like this:

    Dim i As Long

    Dim IE As ObjectDim objElement As ObjectDim objCollection As ObjectSet IE = CreateObject("InternetExplorer.Application")IE.Visible = True

  • 8/2/2019 Web Automation Using Vba

    7/13

  • 8/2/2019 Web Automation Using Vba

    8/13

    Values are stored in a variety of elements inHTML. They can be in tables, textboxes, links,

    and others. Regardless of where the targetvalue is, you need to know how to reference anHTML control. The most common way isprobably by it's ID.

    Find 2 input tags:' 1. Text field'

    '' 2. Button'

  • 8/2/2019 Web Automation Using Vba

    9/13

    How do I put a value into a texbox?Say you want to dynamically put a value in the

    Google search box. You would need to use thiscode.

  • 8/2/2019 Web Automation Using Vba

    10/13

    Set objCollection =IE.document.getElementsByTagName("input")

    i = 0While i < objCollection.Length

    If objCollection(i).Name = "s" Then

    ' Set text for searchobjCollection(i).Value = "india"

    ElseIf objCollection(i).Type = "submit" And _

    objCollection(i).Name = "" Then

    ' "Google Search" button is foundSet objElement = objCollection(i)

    End IfEnd Ifi = i + 1

    Wend

    objElement.Click = i

  • 8/2/2019 Web Automation Using Vba

    11/13

    Wait when IE is busy.

    Clean objects which created.

    Do While IE.Busy

    Application.Wait DateAdd("s", 1, Now)Loop

    ' Show IEIE.Visible = True

    ' Clean upSet IE = NothingSet objElement = Nothing

    Set objCollection = Nothing

    Application.StatusBar = ""End Sub

  • 8/2/2019 Web Automation Using Vba

    12/13

    This example display Google Search results

    for input string India.

  • 8/2/2019 Web Automation Using Vba

    13/13