I wrote the function below. However, when handling the status variable, I get an error NullReference Exception unhandled by user code.
I come from VBA where I did not have to deal with this type of issue on my functions. the status variable is supposed to set my objects properties to true or false.
I tried setting
status = New Boolean
status = False
But nothing worked
Here is my code
Option Strict On
Option Explicit On
Imports Microsoft.Office.Interop.Excel
Imports System.Windows.Forms
Module sheetView
Function viewSheets(sheetName As String, status As Boolean) As String
Dim ThisApplication As Excel.Application = New Excel.Application()
Dim WB As Excel._Workbook
Dim WS As Excel.Worksheet
WB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
WS = CType(WB.Sheets(sheetName), Excel.Worksheet)
With ThisApplication
.ScreenUpdating = False
WS.Select()
.ActiveWindow.DisplayGridlines = status
.ActiveWindow.DisplayHeadings = status
.ActiveWindow.DisplayWorkbookTabs = status
.DisplayFormulaBar = status
.DisplayStatusBar = status
.ExecuteExcel4Macro("Show.ToolBar(""Ribbon"", status)")
.ScreenUpdating = True
End With
Return ""
End Function
End Module
Private Sub btnEmployeeDashboard_Click(sender As Object, e As EventArgs) Handles btnEmployeeDashboard.Click
sheetView.viewSheets("employeeBoard", True)
End Sub
statusvariable is where the problem is? Do you know on which line the error is happening? Is there a reason you are declaringWBas a_Workbookbut castingGlobals.ThisWorkbook.Application.ActiveWorkbookto aWorkbook(no underscore)?