Dear Code Project Community,
I have been searching a lot and didnt find any Solution which worked for me so perhaps someone can give me a hint on my Problem.
Id like to show a Cell-Value in a textbox which can be choosen dependend on inputs of other Textboxes.
For now i know how to do the textboxes themselves and limit them to numerical inputs.
But when i try to open a excel file ,display the specific cell from the selected sheet i always get a NullReferenceException. It is as if my declaration to open the file via xlApp.Workbooks.open(filename) doesn't work and I don't get why?
My code looks like this at the moment:
Private Sub ButtonShow_Click(sender As Object, e As EventArgs) Handles ButtonShow.Click
Dim CellValue As String
Dim filename As String = LabelFilePath.Text
If IO.File.Exists(filename) Then
Dim xlApp As Excel.Application = Nothing
Dim xlWorkbooks As Excel.Workbooks = Nothing
Dim xlWorkbook As Excel.Workbook = Nothing
Dim xlWorksheets As Excel.Worksheets = Nothing
Dim xlWorksheet As Excel.Worksheet = Nothing
Dim xlCells As Excel.Range = Nothing
Try
xlApp = New Excel.Application
xlWorkbooks = xlApp.Workbooks
xlWorkbook = xlApp.Workbooks.Open(filename)
xlWorksheets = xlWorkbook.Sheets
xlWorksheet = xlWorkbook.Sheets(TextBoxSheet)
CellValue = xlWorksheet.Cells(TextBoxRow.Text, TextBoxColumn.Text)
TextBoxCell.Text = CellValue
Catch ex As Exception
MessageBox.Show("Datei nicht vorhanden")
Finally
xlApp.Application.Quit()
End Try
End If
End Sub
End Class
If you need more of my Code or explanation, i'll try to add it as soon as possible. I will also add your Solution to the Code here.
What I have tried:
I tried different approaches too open the file or to get xlApp initiated with code like
xlWorkbook = Excel.Workbooks.OpenXML(filename)
and
xlApp = CreateObject("Excel.Application")
but it got me nowhere. And yes i added
Imports Excel = Microsoft.Office.Interop.Excel
with the right References.
I'm open for any Suggestions and would appreciate any help.
Thanks