VB.Net Excel Addin,如何按名称将数据写入特定工作表?

所以这就是我遇到的问题.我正在将一个旧的Excel宏转换为excel add in,这样我就可以更轻松地与我的同事分享.我是VB.net的新手,但我正在做我能做的事,所以请放轻松我.

我有一个允许用户输入数据的Windows表单,当他们点击输入数据按钮时,数据应该从表单转到特定的工作表.代码如下:

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

(注意我拿出了评论和一些不相关的额外部分,因此下面的详细错误消息包含错误的行号)

我可以从excel中打开窗体,但是当我输入一些数据并单击输入数据时会发生这种情况:

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

在这一行:

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:

解决方法

您无法直接从Excel应用程序中引用工作表.工作表集合是Workbook对象的一部分. Workbooks Collection是Application Object的一部分.

试试这个:

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")

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...