Recent Posts

Tuesday, March 11, 2008

Read from Excel File from VB 6.0

Private Sub LoadExcel(ByVal strSheet As String)

On Error GoTo EErr

'This can be done to directly access excel but you need to add reference for excel

'Dim objExcel As Excel.Application
'Dim objSpread As Excel.Workbook

Dim intRow As Integer

Dim strSNo As String
Dim strEntryNo As String
Dim strFName As String
Dim strMName As String
Dim strLName As String
Dim strFather As String

dim objExcel as object
dim objSpread as object
dim introw as integer

' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSpread = objExcel.Workbooks.Open(strSheet)

' This is done to avoid unnecessary running excel.exe application in memory
' This closes the excel once it is set to nothing after the work is done
objExcel.UserControl = True

'Decide the row from which you want to start reading the excel
introw=1
' Now reading the excel file row by row after opening the excel File

Do Until objExcel.Rows.Count = 113

strEntryNo = Trim(objExcel.Cells(intRow, 2).Value)
strFName = Trim(objExcel.Cells(intRow, 3).Value)
strMName = Trim(objExcel.Cells(intRow, 4).Value)
strLName = Trim(objExcel.Cells(intRow, 5).Value)
strFather = Trim(objExcel.Cells(intRow, 6).Value)
intRow = intRow + 1
Loop

' This is essential to close the excel file
objExcel.Quit
Set objSpread = Nothing
Set objExcel = Nothing

Exit Sub
EErr:
' This is essential to close the excel file
objExcel.Quit
Set objSpread = Nothing
Set objExcel = Nothing
Err.Raise Err.Number, Err.Source, Err.Description


End Sub

Related Posts by Categories




No comments:

Post a Comment