Click here to Skip to main content
15,888,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On the first time when the application starts it works fine apExcel is Microsoft.Exc
apExcel.Workbooks.Open(strSheetName)
After that apExcel is nothing Object reference not set to an instance of an object.
NullReferenceException was unhandled

Public apExcel As Microsoft.Office.Interop.Excel.Application = DirectCast(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
VB
Public Sub LoadSpreadsheet(ByRef cSheetType As String)

        Dim strSheetName As String
        Dim cYear As String
        Dim nMsgResp As Short
        Dim cSheetName As String

         Call CloseSheet()

        cYear = GetTheSurveyYear()
        cSheetName = "frm" & cSheetType & cYear & ".xls"

        strSheetName = DevelopmentStatus("data") & cSheetName

        If Not IsTheFileHere(strSheetName) Then
            nMsgResp = MsgBox("The template for " & cYear & " does not exist.  It will be created from the default template.", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, cMsgBoxHeader)
            Call CopyDatabaseFromDefault(strSheetName, cSheetType & ".xls", cYear)
        End If
        Call CreateBackup(strSheetName, cSheetType & ".xls", cYear, False)

        apExcel.Workbooks.Open(strSheetName)
                apExcel.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized

               If Val(apExcel.Version) < 9 Then
            nMsgResp = MsgBox("Your version of Excel may not support all features of the OES Forms System.", MsgBoxStyle.OKOnly, cMsgBoxHeader)
        End If

                apExcel.CommandBars.ActiveMenuBar.Controls.Item("File").Controls.Item("Save").Enabled = False

                apExcel.DisplayAlerts = False
                apExcel.Application.Visible = True
        lSheetLoaded = True

End Sub
Posted

1 solution

Have you used the magic tool Debugger? Set a break point to this method and review your code line by line.

You are having this exception because you have a null value assigned to one of your variables. I am mostly suspicious at you are getting null value at
C#
DevelopmentStatus("data")
Also it is good to check if you are getting values assigned to cSheetType & cYear.

Good luck,
OI
 
Share this answer
 
Comments
Tony Radu 18-Jan-12 13:31pm    
both have value. should the workbooks be closed that where i found nothing..
Orcun Iyigun 18-Jan-12 13:40pm    
What is the value of strShettName in this line apExcel.Workbooks.Open(strSheetName)Although the values are loaded, the path might not be true? You might need some tweaking in the file path.

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