Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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
VB
xlWorkbook = Excel.Workbooks.OpenXML(filename)

and
VB
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
Posted
Updated 12-Nov-20 4:24am
v3
Comments
Richard MacCutchan 5-Nov-20 11:17am    
You need to explain exactly what errors you get, ands where they occur. If the errors are a result of bad data in a TextBox then you need to examine that data to see what is wrong with it.
Richard MacCutchan 12-Nov-20 7:29am    
And you still have not explained exactly where the NullReferenceException occurs.
LndM 12-Nov-20 8:54am    
The NullReferenceException occurs at xlApp, as i wrote allready, xlApp is still Nothing after i initialise it with New Excel.Application, and stays nothing till it gets a Problem when i Quit it in xlApp.Application.Quit(). The Question is why does it stay Nothing even thought i declared it as an Application and use open() to give it Data.
Richard MacCutchan 12-Nov-20 9:02am    
Most likely because the statement
xlApp = New Excel.Application

does not create a new Excel Application object. Are you sure this code compiled cleanly?
LndM 12-Nov-20 9:40am    
No im not really sure about anything to be honest, why could it compile unclean? But what does create an Excel Application object if not that line?

Your xlApp is never getting initialised and is always Nothing, so when you try to use it VB throws a NullReferenceException.

Add the line 'xlApp = New Excel.Application()' after the 'Try' to initialise xlApp.

NullReferenceException is a very common error and one you need to learn how to debug. Within your IDE when running in debug mode it should highlight the line that the error is on. If you then hover your mouse over the variables on that line you should find one you are referencing that is still set to Nothing - that will be causing your problem.
 
Share this answer
 
Comments
LndM 12-Nov-20 6:46am    
Thanks, I added the line but it doesn't help the Program to run, as it still is Nothing after the initialised line.
This code works for me:
VB
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    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 xlWorkbook As Excel.Workbook = Nothing
            Dim xlWorksheet As Excel.Worksheet = Nothing
            Dim xlRange As Excel.Range = Nothing
            Try
                xlApp = New Excel.Application
                xlWorkbook = xlApp.Workbooks.Open(filename)
                xlWorksheet = xlWorkbook.Sheets("Sheet1")
                Dim row = Int32.Parse(TextBoxRow.Text) ' NB a bad way to convert user input
                Dim col = Int32.Parse(TextBoxColumn.Text) ' ALWAYS use TryParse in live code
                xlRange = xlWorksheet.Cells(row, col)
                CellValue = xlRange.Value
                TextBoxCell.Text = CellValue

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                xlApp.Application.Quit()

            End Try
        End If

    End Sub
End Class

Feel free to modify as you find necessary, although the basic functionality is correct.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900