本文介绍了vb.net中的Excel应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序首次启动时,它可以正常运行apExcel是Microsoft.Exc
apExcel.Workbooks.Open(strSheetName)
在那之后,apExcel就什么都没有了.没有将对象引用设置为对象的实例.
NullReferenceException未处理

作为Microsoft.Office.Interop.Excel.Application的公共apExcel = DirectCast(CreateObject("Excel.Application"),Microsoft.Office.Interop.Excel.Application)

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)

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


结束Sub


End Sub

推荐答案

DevelopmentStatus("data")

同样,最好检查是否要获取分配给cSheetType &的值. cYear.

祝你好运,
OI

Also it is good to check if you are getting values assigned to cSheetType & cYear.

Good luck,
OI


这篇关于vb.net中的Excel应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:10