Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Internet > Unit Conversion

Use Google Calculator for unit conversion

Note: Unlike WinHTTP, X-HTTP and XMLHTTP, the InternetExplorer object retrieves the "interpreted" page instead of the page source code.
This makes it the best, if not the only way to retrieve Google Calculator output, which is a 100% JavaScript generated page.

 

InternetExplorer.Application
VBScript Code:
Function GoogleUnitConversion( myQuery )
' Name:       GoogleUnitConversion
' Function:   Convert units using Google Calculator and Internet Explorer
' Usage:      WScript.Echo GoogleUnitConversion( "10+cm+in+inches" )
' Returns:    10 centimeters = 3.93700787 inches
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
    Dim blnTimedOut, i, j, objIE, objMatches, objRE, strAllText, strMatch, strPattern, strURL
    ' Open the appropriate URL in Internet Explorer
    strURL = "http://www.google.com/search?q=" & myQuery & "&pws=0&hl=en&num=1"
    strPattern = "\)\s*" & Left( myQuery, InStr( myQuery, "+" ) -1 ) &  "\s*[ˆ=]+\s*=\s*[\d\., ]+(\s+[a-z]+)*\s*" _
               & Right( myQuery, Len( myQuery ) - InStrRev( myQuery, "+" ) )
    Set objIE = CreateObject( "InternetExplorer.Application" )
    objIE.Visible = False
    objIE.Navigate2 strURL
    ' Wait till IE is ready
    i = 0
    blnTimedOut = False
    Do While objIE.Busy
        WScript.Sleep 100
        i = i + 1
        ' Time out after 10 seconds
        If i > 100 Then
            blnTimedOut = True
            Exit Do
        End If
    Loop
    ' Retrieve the URL's text
    If Not blnTimedOut Then strAllText = objIE.Document.Body.InnerText
    ' Close the Internet Explorer session
    objIE.Quit
    Set objIE = Nothing
    ' Use a regular expression to extract the result from the web page
    Set objRE = New RegExp
    objRE.Global = True
    objRE.IgnoreCase = True
    objRE.Pattern = strPattern
    Set objMatches = objRE.Execute( strAllText )
    j = objMatches.Count - 1
    If j < 0 Then
        strMatch = ""
    Else
        strMatch = Trim( Mid( objMatches.Item(j).Value, 2 ) )
    End If
    Set objMatches = Nothing
    Set objRE = Nothing
    GoogleUnitConversion = strMatch
End Function
Requirements:
Windows version: Windows 98 or later
Network: any
Client software: Internet Explorer
Script Engine: any
Summarized: Works in Windows 98 and later with Internet Explorer.
 
[Back to the top of this page]
 
Sample usage
VBScript Code:
' Command line arguments assumed in this example: 10 cm in inches
WScript.Echo GoogleUnitConversion( MakeQuery( WScript.Arguments.UnNamed ) )

Function MakeQuery( ByRef objArgs )
    Dim i, strQuery
    If objArgs.Count <> 4 Then Syntax
    If Not IsNumeric( objArgs(0) ) Then WScript.Quit 1
    If UCase( objArgs(2) ) <> "IN" Then WScript.Quit 1
    For i = 0 To objArgs.Count - 1
        strQuery = strQuery & "+" & objArgs(i)
    Next
    MakeQuery = Mid( strQuery, 2 )
End Function
 
Sample output:
10 centimeters = 3.93700787 inches

page last uploaded: 2016-09-19, 14:58