Monday, August 26, 2013

DBConnection Excel Object - QTP

Public Function getDBConnectionExcelObject (strFilePath)
 Dim objConnection, objExcel, strExcelVersion, strVersion
 Set objExcel=CreateObject("Excel.Application")
 strExcelVersion=objExcel.Version
 Set objExcel = Nothing
 Set objConnection = CreateObject("ADODB.Connection")
 If strExcelVersion<=11.0 Then
  objConnection.Provider = "Microsoft.Jet.OLEDB.4.0"
  strVersion="8.0"
 ElseIf strExcelVersion>=12.0 Then
  objConnection.Provider = "Microsoft.ACE.OLEDB.12.0"
  strVersion="12.0"
 End If
 objConnection.ConnectionString ="Data Source= " & strFilePath &";" &"Extended Properties=Excel " & strVersion & ";"
 Set getDBConnectionExcelObject = objConnection
End Function

No comments:

Post a Comment