VB.Net Excel Addin, how to write data to a specific worksheet by name?

So here I have a problem. I am converting an old Excel macro in addition to excel so that I can share it with my colleagues. I am new to VB.net, but I am doing what I can, so please come to me.

I have a Windows form that allows the user to enter data, and when they press the data input button, the data should be sent in the form to a specific worksheet. The code is as follows:

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form_CutListEntry

    Dim xApp As New Excel.Application
    Dim wss As Microsoft.Office.Tools.Excel.Worksheet


    Private Sub Btn_InsertJobInfo_Click(sender As Object, e As EventArgs) Handles Btn_InsertJobInfo.Click
        wss = xApp.Worksheets("Job Info")

        'Check that all data is entered
        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

       'Write data to excel worksheet. 
        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

(Note: I took out the comments and some additional details that are not appropriate, so the following detailed error message has incorrect line numbers)

I can open the Windows form just differently from excel, but when I enter some data and click on the data entry, this happens:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in Toms CutList Maker.dll but was not handled in user code
Additional information: Exception from HRESULT: 0x800A03EC

In this line:

wss = xApp.Worksheets("Job Info")

, - ?

, - :

 System.Runtime.InteropServices.COMException was unhandled by user code
  ErrorCode=-2146827284
  HResult=-2146827284
  Message=Exception from HRESULT: 0x800A03EC
  Source=Microsoft.Office.Interop.Excel
  StackTrace:
       at Microsoft.Office.Interop.Excel.ApplicationClass.get_Worksheets()
       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 15
       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: 
+4
2

Excel. " ". .

:

Dim xApp As New Excel.Application 
Dim wss As Microsoft.Office.Tools.Excel.Worksheet
Dim wb As Microsoft.Office.Tools.Excel.Workbook

....

wb = xApp.Workbooks("My Workbook.xlsx") 'replace with your workbook name and proper file extension
wss = wb.Worksheets("Job Info")
0

Excel. , , :

Dim xlWorkBook As Excel.Workbook = xlapp.Workbooks.Add(Application.StartupPath + "\My Workbook.xlsx")
xlWorkBook.Sheets.Add("Job Info")
Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Sheets("JobInfo")

, , , .

0

Source: https://habr.com/ru/post/1616917/


All Articles