Trying to create a small report program in VB.Net. It works in Excel 2003 and 2010, but throws an error in Excel 2013 when trying to create the second worksheet. The Procedure is listed below, I also point out the line where it errors. It doesn't even catch in the Try and Catch that I added it comes across as an "Unhandled Exception".
The target framework is 3.5
Have tried x86, x64, and Any CPU as the target CPU
I used the 2003 and 2010 .dlls as my reference.
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"
This is the error that I receive.
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)