0

我写了下面的函数。但是,在处理状态变量时,我收到一个错误 NullReference Exception 未由用户代码处理。

我来自 VBA,我不必在我的函数中处理这类问题。状态变量应该将我的对象属性设置为真或假。

我试过设置

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
4

1 回答 1

1

NullReference在没有对象实例的情况下尝试访问类成员时会发生A。

换句话说:其中一个值 ( ThisApplication/ WB/ WS/ WS.ActiveWindow) 为空。在调试器中单步调试您的代码以找出是哪一个。

于 2013-07-06T15:48:38.840 回答