Wednesday 25 September 2013

Reading an excel file using Microsoft excel object in Visual Basic.

option explicit
Private exc as Object
Private wb as Object
Private ws as Object

Private sub ImportExcelData(excelFileName as String)
    if exc is nothing then
       set exc = CreateObject("Excel.Application")
   end if
   if exc is nothing then
        call msgbox("Failed to load excel application")
        exit sub
   end if
   set wb = exc.Workbook.Open(excelFileName)
   if wb is nothing then
        call msgbox("Failed to open excel file")
        exit sub
   end if

' please note that this program is for file without password, open file with password will be on the
 ' next blog
  set ws = wb.Sheets("sheet1")

   if ws is nothing then
        call msgbox("Failed to load worksheet")
        exit sub
   end if
   dim r as long
   r=1
   while not ws.Range("A" & r ":A" & r).value<> ""
      debug.print  ws.Range("A" & r ":A" & r).value
      r=r+1
   wend
   wb.close true
   set wb=nothing
   exc.quit
   set exc=nothing
End Sub

No comments: