开发者

ExcelADO - Using Range to fetch data from excel using VBScript

开发者 https://www.devze.com 2022-12-19 16:59 出处:网络
Given the advantages of ExcelADO as described here I have decided to use Excel ADO for QTP Automation. So here is the code which I used -

Given the advantages of ExcelADO as described here I have decided to use Excel ADO for QTP Automation. So here is the code which I used -

'Open the ADO connection to the Excel workbook
Dim oConn
Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=D:\Mine\QTP\Book1.xls;" & _
           "Extended Properties=""Excel 8.0;HDR=NO;"""  


'Set Record Set Object       
Dim oRS 
Set oRS = CreateObject("ADODB.Recordset")

'Execute Query 
oRS.Open "Select * from qwerty", oConn, adOpenStatic 

'Get String
a = oRs.GetString() 

In the query mentioned above 'qwerty' is the name of cell range in Excel Sheet. Above mentioned piece of code works perfect provided cell range is name of two or more cells. I find it very useful to use named cell range instead of using 'sheetname' and/or 'row and column positions'

Now when I name just one cell and use above piece of code then following exception is thrown

" Microsoft Jet database engine could not find object 'qwerty'. Make sure object exists and that you spell its name and p开发者_JS百科ath name correctly

Code: 80040E37

"

It looks to me data can be fetched using range (named cells) only when range utilizes 2 or more cells. Is it correct? If so then how can I fetch data for only one named cell?

~ T


Both of the example below work for me, as you can see, it is very similar to your example except for IMEX=1, which may or may not make a difference.

MsgBox fExcelCellADO ("c:\docs\book1.xls","sheet1$b2:b2")
MsgBox fExcelCellADO ("c:\docs\book1.xls","therange")

Function fExcelCellADO(strFileName, strCell)

    Dim cn 
    Dim rs 

    Set cn = CreateObject("ADODB.Connection")
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
             & "Data Source=" & strFileName & ";" _
             & "Extended Properties='Excel 8.0;HDR=No;IMEX=1';"

    Set rs = CreateObject("ADODB.Recordset")
    rs.Open "SELECT F1 FROM [" & strCell & "]", cn
    fExcelCellADO = rs.fields("F1")

    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号