vb.net中的Excel应用程序 [英] Excel Application in vb.net

查看:92
本文介绍了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

推荐答案

您是否使用了魔术工具Debugger?为该方法设置一个断点,并逐行查看您的代码.

之所以会出现此异常,是因为您为变量之一分配了一个空值.我最怀疑的是,您在
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
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应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆