0
我写了下面的函数。但是,在处理状态变量时,我得到一个用户代码未处理的错误NullReference Exception。在函数中处理NullReference异常
我来自VBA,在这里我不需要处理这种类型的问题。状态变量应该将我的对象属性设置为true或false。
我试着设置
status = New Boolean
status = False
但是毫无效果
这里是我的代码,当你试图在没有对象实例来访问类成员发生
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
你确定'status'变量是问题所在吗?你知道错误发生在哪一行吗?是否有一个原因,你声明'WB'为'_Workbook',但将'Globals.ThisWorkbook.Application.ActiveWorkbook'转换为'Workbook'(没有下划线)? – JLRishe
状态变量正在分配给一个空引用,所以@JLRishe是正确的,很难说没有通过调试器进行失败的地方,你能发布堆栈跟踪吗? –