Thursday, July 08, 2010

IE8 close tab programmatically

Sometimes you may wish to close a browser instance and Twebst Library has support for that. You just have to call Close method against the Browser web automation object you want to close.

Let's see a short JScript web automation macro that closes the first IE8 tab loaded from domain. If the closing tab is the last tab inside the IE frame then the whole browser will be closed.
// Create core object.
var core = new ActiveXObject("Twebst.Core");

// Find a browser for which the displayed URL contains "".
core.useRegExp = true;
var b = core.FindBrowser("url=.*");
There are more Twebst methods for automating IE7 and IE8 tabs that I'll talk about in future articles: OpenNewTab, activeTab, isTabActive, tabList

Wednesday, July 07, 2010

Twebst Automation Studio 2.5 - New Release!

This blog was too quiet for some time but after another development cycle I am happy to announce a new release: Twebst Automation Studio version 2.5

New features:


Download Twebst - IE Automation Software

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("")

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("")
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

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.

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.


Thursday, February 25, 2010

Extract web data into Excel.Application with browser macro

Let's continue my previous post where I presented a browser macro that automatically translates phrases in an Excel document using Google Translate tool. As you cand see in code, the WSH script performs the following steps:
  • instantiate "Twebst.Core" web automation object and start an IE browser.
  • instantiate "Excel.Application" automation object and open XLS document.
  • get a phrase to be translated from Excel document.
  • navigate to Google translate tool web page.
  • fill out web controls (the text to be translated, source and target language combo-boxes) and then automatically press submit button.
  • wait the web page to load, get the translated text and save it into Excel document.

Let's see some code. I will skip the initial setup phase and go directly to web automation part which is more interesting:

for (var i = 1; i <= 5; ++i)
// Get current text to translate from XLS document.
var textToTranslate = excelApp.ActiveSheet.Cells(i, 1).Formula;

// Web automation part implemented with Twebst Automatio Studio.
browser.FindElement('textarea', 'id=source').InputText(textToTranslate);
browser.FindElement('select', 'name=sl').Select('English');
browser.FindElement('select', 'name=tl').Select('French');
browser.FindElement('input submit', 'text=Translate').Click();

// Get the reszult and save it into XLS document.
var translatedText = browser.FindElement('span', 'id=result_box').text;
excelApp.ActiveSheet.Cells(i, 2).Formula = translatedText;

Points of interest:

  • FindElement method waits for the IE web page to be completely loaded before searching the element. The timeout value can be changed programatically (see loadTimeout, searchTimeout properties).
  • InputText method sets the specified value in edit/password web controls and fires all necessary HTML events (onkeydown, onkeypress, onkeyup, onchange, etc).
  • Select method fully automates combo-boxes and list-boxes web control and fires all necessary HTML events (like onchange).


Wednesday, February 24, 2010

Excel and IE automation - the WSH way

I can think of two possible scenarios when automating Excel and Internet Explorer makes any sense:
  • you have data stored in Excel sheets and you want to push data into online web forms.

  • you need to extract data from web and save them into Excel xls file.
There are two ways to implement this:
  • automate Internet Explorer browser from Excel VBA macro.

  • automate both IE and Excel from an external process using OLE automation.
Today I will talk about the second option; I will automate both Internet Explorer and Excel application from a WSH script using Twebst Automation Studio. Here's a diagram showing the work-flow:

Fill out web forms with Excel data

The WSH web macro in this tutorial opens an XLS file containing phrases to be translated using Google Translate tool. It then automatically gets each phrase, opens an IE browser, navigates to translation page, fills out the text to be translated, press submit button, gets back the result and saves it into the same XLS file.

That's all for now! (as I try to keep articles relatively short) Meanwhile, you should download Twebst Automation Studio and the web macro sample to see it at work. Then, you can continue with the next article explaining the web macro code and comments: Extract web data into Excel.Application with browser macro


Twebst Automation Studio (FREE edition available btw)

Friday, February 05, 2010

IE8 automation with Excel VBA macro

Twebst Automation Studio Ver 2.1 New Release!

Many people requested for VBA (Excel, Word) direct web automation support in Twebst Library. The previous version could not be easily used from VBA because of the way Excel works with methods with variable number of parameters (which is the case for many Twebst methods like: FindElement, FindBrowser, etc).

The new Twebst 2.1 version includes a new full set of interfaces and methods specially designed for Excel VBA. The web recorder was also updated to generate VBA code that you can easily include in your Excel macro. To see what's new in Twebst ver 2.1 check changelog.txt

Automating Internet Explorer from Excel VBA macro is now possible with Twebst Automation Studio 2.1.