Saturday, October 15, 2011

VB.Net 2010 and MS Excel 2007 (Code to access Excel File using vb.Net2010)

'Add reference to Microsoft.Office.Interop.Excel

Dim ExcelApp As New Microsoft.Office.Interop.Excel
Dim WSheet As New Microsoft.Office.Interop.Excel

'if the code above does not work then declare Wsheet as
'Dim WSheet As Microsoft.Office.Interop.Excel


Dim ExcelProcess As New Process

Try
WSheet = ExcelApp.Sheets("Sheet1")

Catch ex As Exception

ExcelProcess.Start(Application.StartupPath & "\reports\excelFile.xlsx")
WSheet = ExcelApp.Sheets("Sheet1")
ExcelApp.Visible = True
End Try

WSheet.Select()

MsgBox(WSheet.Range("C6:C6").Value)


Try
ExcelApp.Quit()
Catch ex As Exception
End Try

0 comments: