Friday, April 9, 2010

VBScript - Use ADO to Query an Excel Workbook

VBScript - Use ADO to Query an Excel Workbook

'* Script name: AdoExcel.vbs
'* Created on: 2006-07-08 23:34:36
'* Author: Kent Finkle
'* Purpose: Demonstrates the use of ADO SQL to query an Excel
'* workbook.

Option Explicit

Const adOpenStatic = 3
Dim objConnection
Dim objRecordset

Set objConnection = CreateObject("ADODB.Connection")

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\scripts\Book1.xls;" & _
"Extended Properties=""Excel 8.0;"""

Set objRecordset = CreateObject("ADODB.Recordset")
objRecordset.Open "Select * from [Sheet1$]", objConnection, adOpenStatic
Wscript.Echo objRecordset.RecordCount
objRecordset.Close
objConnection.Close

[Via]