Click here to Skip to main content
15,885,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form_CutListEntry
    'Local Dims for this Class. 
    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

'input data into spreadsheet
        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:

VB
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 :
VB
wss = wb.Worksheets("Job Info")


I have tried changing the line to:
VB
wss = wb.ActiveSheet


But the same error pops up. Any ideas or links to articles or anything would be appreciated.
Posted

1 solution

I don't know for VB.Net, but for VBA, you need to use set to save an object in a variable
VB
set wss = wb.Worksheets("Job Info")

VB
set wss = wb.ActiveSheet

Do you really need to translate the old macro ?
Did you try to save you excel workbook as an .xla file by choosing the file type ?

Otherwise, you define wss in a class, but you did not instantiate before using it.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900