So here's the problem I have. I am converting an old Excel macro into an excel add in so I can share it easier with my coworkers. I am new to VB.net but I'm doing what I can so please go easy on me.
I have a Windows form that allows a user to enter data and when they hit the enter data button the data is supposed to go form the form to a specific worksheet. The code is as follows:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form_CutListEntry
Dim xApp As Excel.Application = GetObject(, "Excel.Application")
Dim wb As Microsoft.Office.Tools.Excel.Workbook = xApp.ActiveWorkbook
Dim wss As Microsoft.Office.Tools.Excel.Worksheet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Btn_InsertJobInfo_Click(sender As Object, e As EventArgs) Handles Btn_InsertJobInfo.Click
wss = wb.Worksheets("Job Info")
If Trim(TxtBx_CustomerName.Text) = "" Then
TxtBx_CustomerName.Focus()
MsgBox("Please enter a Customer Name")
Exit Sub
End If
If Trim(TxtBx_OrderNum.Text) = "" Then
TxtBx_OrderNum.Focus()
MsgBox("Please enter an Order Number")
Exit Sub
End If
If Trim(TxtBx_CutlistAuthor.Text) = "" Then
TxtBx_CutlistAuthor.Focus()
MsgBox("Please enter your initials")
Exit Sub
End If
wss.Cells(3, 1) = "Customer Name: " + TxtBx_CustomerName.Text
wss.Cells(4, 1) = "Order Number: " + TxtBx_OrderNum.Text
wss.Cells(5, 1) = "Todays Date: " + TxtBx_TodaysDate.Text
wss.Cells(6, 1) = "Cutting List Prepared By: " + TxtBx_CutlistAuthor.Text
Exit Sub
End Sub
This gives me the following error when I try and run it:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Toms CutList Maker
StackTrace:
at Toms_CutList_Maker.Form_CutListEntry.Btn_InsertJobInfo_Click(Object sender, EventArgs e) in d:\tom\documents\visual studio 2013\Projects\Toms CutList Maker\Toms CutList Maker\CutList Entry.vb:line 18
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.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.OnMessage(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)
InnerException:
That error is on the line :
wss = wb.Worksheets("Job Info")
I have tried changing the line to:
wss = wb.ActiveSheet
But the same error pops up. Any ideas or links to articles or anything would be appreciated.