对象未设置为对象的实例错误Excel 2013自动化VB.NET

试图在VB.Net中创build一个小的报告程序。 它可以在Excel 2003和2010中使用,但是在尝试创build第二个工作表时会在Excel 2013中引发错误。 下面列出程序,我也指出它出错的地方。 它甚至没有抓住Try和Catch,我把它添加为“未处理的exception”。

目标框架是3.5

已尝试x86,x64和任何CPU作为目标CPU

我用2003和2010的.dlls作为我的参考。

Imports Microsoft.Office.Interop.Excel Private Sub DRpts() Try Dim strFileDirName As String = "" Me.Cursor = Cursors.WaitCursor 'Morning Report Sheet 1 Dim MornRpt As String = "SELECT DateCreated, ReportClass, SUM(QtyOrdered), SUM(ExtendedPrice) " & _ "FROM tmp_DailyReport GROUP BY DateCreated, ReportClass" Dim oExcel As Object Dim oBook As Object Dim oSheet1 As Object Dim oSheet2 As Object oExcel = CreateObject("Excel.Application") oBook = oExcel.workbooks.add oSheet1 = oBook.Worksheets("Sheet1") oSheet1.Name = "Morning Report" 'Load Headers oSheet1.Range("A1").Value = "Class" oSheet1.Range("B1").Value = "Units" oSheet1.Range("C1").Value = "Extended Price" 'Formatting() oSheet1.Range("A1:C1").Font.Bold = True Dim CellLoc As String = "" Dim CellCntr As Integer = 2 Dim SRptDate As String = Me.RptStart.Text Dim ERptDate As String = Me.RptEnd.Text Dim Reader1 As Odbc.OdbcDataReader Using connection As New Odbc.OdbcConnection("DSN=#########;") Dim Command As New Odbc.OdbcCommand(MornRpt, connection) Command.Connection.Open() Reader1 = Command.ExecuteReader While Reader1.Read() CellLoc = "A" & CellCntr oSheet1.Range(CellLoc).Value = Reader1.GetValue(1).ToString CellLoc = "B" & CellCntr oSheet1.Range(CellLoc).Value = Reader1.GetValue(2).ToString CellLoc = "C" & CellCntr oSheet1.Range(CellLoc).Value = Reader1.GetValue(3).ToString CellCntr = CellCntr + 1 End While End Using Dim FormatRange As String = "C2:C" & CellCntr oSheet1.Range(FormatRange).NumberFormat = "#,##0.00" oSheet1.Columns.EntireColumn.AutoFit() oSheet1.Columns("A").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft ------> oSheet2 = oBook.Worksheets("Sheet2") *** BAD INDEX ERROR OCCURS HERE *** <------------------ ------> ' Also tried this variation --> *** oSheet2 = oBook.Worksheets("Sheet2") *** Neither Work oSheet2.Name = "Summary" Dim OverseasSales As String = "SELECT SUM(QtyOrdered) AS Units, SUM(ExtendedPrice) AS ExtPrice " & _ "FROM dbo.tmp_DailyReport " & _ "WHERE (CustomerNo BETWEEN '0' AND '9)' OR " & _ "CustomerNo BETWEEN '0' AND '9' OR " & _ "CustomerNo = '######')" & _ "GROUP BY DateCreated " 'Load(Headers) oSheet2.Range("A1").Value = "Report Class" oSheet2.Range("B1").Value = "Product" oSheet2.Range("C1").Value = "Units" oSheet2.Range("D1").Value = "ExtendedPrice" 

这是我收到的错误。

 System.NullReferenceException: Object reference not set to an instance of an object. at GoldenManagement.DailyReport.DRpts() at GoldenManagement.DailyReport.RunDailyRpt2_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

我忘了我发布了这个。 我前一段时间find了解决scheme。

发生错误的地方,我应该使用;

 oSheet2 = CType(Me.Application.Worksheets.Add(), Excel.Worksheet) oSheet2.Name = "Sheet2" 

除非另有说明,否则较新版本的Office将默认为书中的一页。