【问题标题】:VB.Net Excel Addin, how to write data to specific worksheet by name?VB.Net Excel Addin,如何按名称将数据写入特定工作表?
【发布时间】:2016-02-24 10:29:26
【问题描述】:

这就是我遇到的问题。我正在将旧的 Excel 宏转换为 Excel 插件,以便我可以更轻松地与我的同事分享。我是 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

(注意我取出了 cmets 和一些不相关的额外部分,因此下面的详细错误消息有错误的行号)

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

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: 

【问题讨论】:

  • Worksheet Object 不是Excel Application Object 的成员,但它是Workbook Object 的成员,后者是Excel Application Object 的成员。所以首先,设置Workbook,然后设置Worksheet

标签: vb.net excel vba


【解决方案1】:

您直接从 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")

确保您尝试打开的工作簿存在。

【讨论】:

    【解决方案2】:

    您不能直接从 Excel 应用程序中引用工作表。工作表集合是 Workbook 对象的一部分。工作簿集合是应用程序对象的一部分。

    试试这个:

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

    【讨论】:

    • 好吧,所以我尝试了这个,这就是我写它的方式:'wb = xApp.Workbooks("CUTLISTMAKER") wss = wb.Worksheets("Job Info")' 无论出于何种原因,我都得到了类似的错误,但它说索引无效。我也尝试使用 wb = xApp.ActiveWorkbook ,这也给了我一个错误。
    • 尝试“CUTLISTMAKER.xlsx”或任何适合您文件的文件扩展名
    • 不幸的是,仍然出现同样的错误,该错误不在 Job Info 行上,而是在新的 wb=xApp.Workbooks 行上。 .老实说,如果我可以改变工作簿的名称,我宁愿让它与 ActiveWorkbook 一起使用。如果我将其更改为: wb = xApp.ActiveWorkbook 然后我在 wss=wb.Worksheets("Job Info") 行上收到“System.NullReferenceException”错误
    • wb = ThisApplication.Workbooks("CUTLISTMAKER.xlsx")怎么样
    • 告诉我“ThisApplication”未声明,我可以继续声明它,但我们会回到第一方对吗?还是有不同的声明方式?
    猜你喜欢
    • 2021-12-12
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2018-03-05
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    相关资源
    最近更新 更多