Sunday, March 07, 2010

Move data from Excel to IE and back with VBA web macros

Let's continue my previous post (IE8 Automation using Excel VBA Macro) with some VBA Internet Explorer automation code:
' Don't forget to add reference to TwebstLib from menu Tools/References
Dim core As TwebstLib.ICoreVba
Set core = New TwebstLib.core

Dim browser As TwebstLib.IBrowserVba
Set browser = core.StartBrowserVba("http://www.google.com/")

For i = 1 To 5
' Get the current text to be translated
Dim textToTranslate As String
textToTranslate = ActiveSheet.Cells(i, 1).Formula

' Twebst web automation part
Call browser.Navigate("http://www.google.com/language_tools?hl=en")
Call browser.FindElementVba("textarea", "id=source").InputText(textToTranslate)
Call browser.FindElementVba("select", "name=sl").Select("English")
Call browser.FindElementVba("select", "name=tl").Select("French")
Call browser.FindElementVba("input submit", "text=Translate").Click

' Find result span object
Dim resultObj As TwebstLib.IElementVba
Set resultObj = browser.FindElementVba("span", "id=result_box")

' Get the text result
Dim translatedText As String
translatedText = resultObj.Text

' Save the result into Excel sheet
ActiveSheet.Cells(i, 2).Formula = translatedText
Next

Points of interest:
  • in VBA web macro new methods are used (see FindElementVba). The newly added methods (from version 2.1) are especially designed for VBA language and they work the same as they non-VBA counterparts except the fact no variable number of search conditions are supported.
  • Twebst web recorder was updated to automatically generate VBA macro code against the newly added methods.
  • all web page loading synchronization is provided by default by the library.
  • when automating web controls all necessary HTML events are fired up against target object (see fireEvent) to simulate user's web action.
Downloads:

Wednesday, March 03, 2010

IE8 Automation using Excel VBA Macro

In my previous posts (Excel and IE automation - the WSH way, Extract web data into Excel.Application with browser macro) I automated both IE and Excel using WSH. Now I'm going to automate Internet Explorer directly from an Excel macro using the VBA language.

The VBA macro will work against a sheet containing phrases to be translated using Google Translate tool. It automatically gets each phrase, opens IE browser, navigates to translation page, waits the web page to complete loading, fills out the text to be translated into HTML web controls, press submit button, gets back the result and saves it into the same Excel sheet.

It is possible to directly automate IE browser from VBA macro but it is not always as simple as it should be. (see my older post: What's wrong with Internet Explorer Automation?) Using Twebst Automation Studio when automating IE from VBA has some advangates:
  • it works with all Internet Explorer versions: IE6, IE7, IE8 and with IE protected mode.

  • it works on all Windows versions: XP, Vista, Win7

  • easily search of elements across all sub-documents inside frames/iframes hierarchy

  • search HTML elements using regular expressions

  • simulate user actions on all HTML controls (button, combo-box, list-box, edit-box, upload control) and fire all necessary HTML events

  • auto-generate VBA code with powerful web recorder

  • much more (see Twebst Automation Studio website)

That's all for now. Until our next encounter, check the download section below and have fun with the Excel VBA macro code.

Downloads: